Sequence Statements are statements that are executed in a sequential manner, one after another, in the order they appear in the code.
In programming, sequence statements are the most basic type of statements and are used to perform a series of operations or tasks. Each statement is executed one at a time, and the flow of execution moves from the first statement to the next until all statements have been executed.
Sequence statements are the simplest type of control structure in programming where each line of code is executed one after the other.
Think of it like a recipe: you can't eat the cake before you bake it, and you can't bake it before you mix the ingredients.
For example, the steps to calculate the area of a rectangle are:
When building such an interactive program to calculate the area, you must follow the following steps:
As a programmer, you must know the syntax to use to:
The syntax to accomplish these varies from one programming language to another.
We've already learned about variable declaration and initialization from our previous lessons.
The syntax for displaying output in C++ is the std::cout function.
The text to display is specified as a string and written between a double quote (") or single quote (')
For example, to display the message "Hello, World!" in C++, you can use the following code:
std::cout << "Hello, World!" << std::endl;
To display "WELCOME TO C++ PROGRAMMING" in C++, you can use the following code:
std::cout << "WELCOME TO C++ PROGRAMMING" << std::endl;
In the above program to calculate the area of a rectangle, to display "Enter length:", you can use the following code:
std::cout << "Enter length: " << std::endl;
To display "Enter width:", you can use the following code:
std::cout << "Enter width: " << std::endl;
We now have the necessary syntax knowledge to write our program to calculate the area of a rectangle in C++.
Since the length and width could be decimal values, it is appropriate to store them as floats.
As we have already discussed in our previous lesson, you can write using namespace std to avoid writing std:: before each standard library function:
#include <iostream>
using namespace std;
int main() {
float length, width; /* Declare variables to store length and width */
cout << "Enter length: "; /* Prompt the user to enter the length */
cin >> length; /* Read the length from the user and store it in the variable 'length' */
cout << "Enter width: "; /* Prompt the user to enter the width */
cin >> width; /* Read the width from the user and store it in the variable 'width' */
cout << "The area of a rectangle of length " << length << " cm and width " << width << " cm is " << length * width << " cm square" << endl;
return 0;
}
Notes:
In order not to be confused, remember that cout is less than and used for output and cin is greater than and used for input.
Note: The code to be executed should be placed between the opening and closing braces of the main function before the return statement.
The explanatory statements are called comments which are ignored by the compiler.
You can run the code in your IDE or use the c++ online compiler below to test the program:
https://www.programiz.com/cpp-programming/online-compiler/
Write a program in C++ to calculate the circumference and area of a circle.
Notes:
#define CONSTANT_NAME VALUE_OF_CONSTANT or const DATA_TYPE CONSTANT_NAME = VALUE;#define PI 3.142 or const float PI = 3.142;The sequence of steps to solve this problem is as follows:
Note: for C++, the get input is in three steps:
cout)cin)Square is a number multiplied by itself and multiplication in programming is denoted by the * operator. Hence, r2 = r * r.
#include <iostream>
#define PI 3.142
using namespace std;
int main() {
float radius, circumference, area; /* Declare variables to store radius, circumference, and area */
std::cout << "Enter radius: "; /* Prompt the user to enter the radius */
std::cin >> radius; /* Read the radius from the user and store it in the variable 'radius' */
circumference = 2 * PI * radius; /* Calculate the circumference using the formula C = 2 * π * r */
area = PI * radius * radius; /* Calculate the area using the formula A = π * r^2 */
std::cout << "The circumference of a circle with radius " << radius << " cm is " << circumference << " cm" << std::endl;
std::cout << "The area of a circle with radius " << radius << " cm is " << area << " cm square" << std::endl;
return 0;
}
Alternatively, we could declare the constant using the const keyword:
#include <iostream>
const float PI = 3.142;
using namespace std;
int main() {
float radius, circumference, area; /* Declare variables to store radius, circumference, and area */
cout << "Enter radius: "; /* Prompt the user to enter the radius */
cin >> radius; /* Read the radius from the user and store it in the variable 'radius' */
circumference = 2 * PI * radius; /* Calculate the circumference using the formula C = 2 * π * r */
area = PI * radius * radius; /* Calculate the area using the formula A = π * r^2 */
cout << "The circumference of a circle with radius " << radius << " cm is " << circumference << " cm" << endl;
cout << "The area of a circle with radius " << radius << " cm is " << area << " cm square" << endl;
return 0;
}
In the above C++ program, the circumference and area are stored before being displayed to the user.
In case you decide not to store but calculate directly, the code will be:
#include <iostream>
#define PI 3.142
using namespace std;
int main() {
float radius; /* Declare variable to store radius */
cout << "Enter radius: "; /* Prompt the user to enter the radius */
cin >> radius; /* Read the radius from the user and store it in the variable 'radius' */
cout << "The circumference of a circle with radius " << radius << " cm is " << 2 * PI * radius << " cm" << endl;
cout << "The area of a circle with radius " << radius << " cm is " << PI * radius * radius << " cm square" << endl;
return 0;
}