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 will consider the syntax in C and Python programming languages.
We've already learned about variable declaration and initialization in C and python from our previous lessons.
The syntax for displaying output in C is the printf 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:
printf("Hello, World!");
To display "WELCOME TO C PROGRAMMING" in C, you can use the following code:
printf("WELCOME TO C PROGRAMMING");
In the above program to calculate the area of a rectangle, to display "Enter length:", you can use the following code:
printf("Enter length:");
To display "Enter width:", you can use the following code:
printf("Enter width:");
Placeholders are used in the printf function to display the values stored in variables.
%d - for integers%f - for floating-point numbers%c - for characters%s - for stringsAfter the format string, you specify the variables whose values you want to display in the order the placeholders appear.
For example, if we've named the memory to store the length as length, we can display its value using the following code:
printf("Length: %d", length);
If we've named the memory to store the width as width, we can display its value using the following code:
printf("Width: %d", width);
To display the area of the rectangle, we can use the following code:
printf("Area: %d", length * width);
In the above code, the placeholder is replaced by the result (length x width) to display the area
You can use multiple placeholders in a single printf statement to display multiple variables or values. You must specify the variables in the same order as the placeholders appear.
For example if the value of length in the memory is 5 and the width is 3 and we wish to display, "The area of a rectangle of length 5 cm and width 3 cm is 15 cm square", assuming the variables are named length and width, we could write the code below:
printf("The area of a rectangle of length %d cm and width %d cm is %d cm square", length, width, length * width);
In the above code, the placeholders are replaced by the values of the variables in the same order as they appear in the format string.
Note: %d is a placeholder for an integer value.
If our data types for length and width are floats, we would use %f instead of %d.
The code will then be:
printf("The area of a rectangle of length %f cm and width %f cm is %f cm square", length, width, length * width);
In C, you can use the scanf() function to read input from the user.
The general syntax for scanf is:
scanf("placeholder", &variable);
In the above code, the placeholder specifies the type of data to read, and the ampersand (&) is used to pass the address of the variable where the input value will be stored.
The placeholders for scanf are:
%d - for integers%f - for float values%lf - for double values%c - for charactersFor example, to read an integer value from the user and store it in a variable named length, you can use the following code:
scanf("%d", &length);
Similarly, to read a float value from the user and store it in a variable named width, you can use the following code:
scanf("%f", &width);
Similarly, to read a double value from the user and store it in a variable named salary, you can use the following code:
scanf("%lf", &salary);
Note the use of %lf instead of %f for the double data type. If you use %f, your readings won't be accurate. Likewise if you use %lf for float data type, your readings won't be accurate. You should pay much attention to the data type you are using (float or double) when you are using the placeholder in the scanf for the decimals.
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 and use the %f placeholder for reading these values.
#include <stdio.h>
int main() {
float length, width; /* Declare variables to store length and width */
printf("Enter length: "); /* Prompt the user to enter the length */
scanf("%f", &length); /* Read the length from the user and store it in the variable 'length' */
printf("Enter width: "); /* Prompt the user to enter the width */
scanf("%f", &width); /* Read the width from the user and store it in the variable 'width' */
printf("The area of a rectangle of length %.2f cm and width %.2f cm is %.2f cm square", length, width, length * width);
return 0;
}
Note: The code to be executed should be placed between the opening and closing braces of the main function before the return statement.
If we use the double data type instead of float, we would use the %lf placeholder for reading these values.
The code will then be:
#include <stdio.h>
int main() {
double length, width; /* Declare variables to store length and width */
printf("Enter length: "); /* Prompt the user to enter the length */
scanf("%lf", &length); /* Read the length from the user and store it in the variable 'length' */
printf("Enter width: "); /* Prompt the user to enter the width */
scanf("%lf", &width); /* Read the width from the user and store it in the variable 'width' */
printf("The area of a rectangle of length %.2lf cm and width %.2lf cm is %.2lf cm square", length, width, length * width);
return 0;
}
The explanatory statements are called comments which are ignored by the compiler.
You can use the c online compiler below to test the program:
https://www.programiz.com/c-programming/online-compiler/
Write a program in C to calculate the circumference and area of a circle.
Notes:
#define CONSTANT_NAME VALUE_OF_CONSTANT#define PI 3.142The sequence of steps to solve this problem is as follows:
Note: for C, the get input is in three steps:
printf)scanf)#include <stdio.h>
#define PI 3.142
int main() {
float radius, circumference, area; /* Declare variables to store radius, circumference, and area */
printf("Enter radius: "); /* Prompt the user to enter the radius */
scanf("%f", &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 */
printf("The circumference of a circle with radius %.2f cm is %.2f cm\n", radius, circumference);
printf("The area of a circle with radius %.2f cm is %.2f cm square", radius, area);
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 <stdio.h>
#define PI 3.142
int main() {
float radius; /* Declare variable to store radius */
printf("Enter radius: "); /* Prompt the user to enter the radius */
scanf("%f", &radius); /* Read the radius from the user and store it in the variable 'radius' */
printf("The circumference of a circle with radius %.2f cm is %.2f cm\n", radius, 2 * PI * radius);
printf("The area of a circle with radius %.2f cm is %.2f cm square", radius, PI * radius * radius);
return 0;
}
Notes: