Control Structures in Programming Languages: Sequencing
Control Structures in Programming Languages: Sequencing
Sequencing
Sequencing or sequential logic depends on a series of instructions that are executed one after the other.
Example: Baking Bread
Add flour.
Add salt.
Add yeast.
Mix.
Add water.
Knead.
Let rise.
Bake.
Selection
Selection or conditional logic involves a number of conditions or parameters which decides one out of several
written modules. These structures can be of three types:
1. Single Alternative
if (condition){
statement (s);
}
For example: Write a program in C that prints “Thank you!” if the user enters an even number.
#include <stdio.h>
int main(){
int n;
printf("Enter any number: ");
scanf("%d", &n);
if (n%2==0)
printf ("Thank you!");
return 0;
}//main
2. Double Alternative
if (condition){
statement (s);
}
else {
statement (s);
}
For example: Write a program in C that prints “Thank you!” if the user enters an even number, and “Ugh!
You’re annoying” if the user enters an odd number.
int main(){
int n;
printf("Enter any number: ");
scanf("%d", &n);
if (n%2==0){
printf ("Thank you!");
}
else
{
printf("Ugh. You're annoying");
}
return 0;
}//main
3. Multiple Alternatives
if (condition){
statement (s);
}
else if {
statement (s);
}
else if {
statement (s);
}
else if {
statement (s);
}
else {
statement (s);
}
For example: Write a program in C that prompts the user to enter a number. If the number is between 1 to 12,
the program outputs the name of the month that corresponds with the number
(E.g. 1 will produce “January”). If any other number is entered, an appropriate error
message is displayed.
#include <stdio.h>
int main()
{
int n;
printf("Enter any number: ");
scanf("%d", &n);
if (n == 1)
printf("January");
else if (n == 2)
printf("February");
else if (n == 3)
printf("March");
else if (n == 4)
printf("April");
else if (n == 5)
printf("May");
else if (n == 6)
printf("June");
else if (n == 7)
printf("July");
else if (n == 8)
printf("August");
else if (n == 9)
printf("September");
else if (n == 10)
printf("October");
else if (n == 11)
printf("November");
else if (n == 12)
printf("December");
else
printf("Invalid Entry!");
return 0;
} //main
Note: Many IF-statements together can become convoluted. C offers a cleaner alternative to nested if-
statements, the switch-case construct which is also a conditional construct. The syntax is as follows:
switch (expression)
{
case constant1:
// statements
break;
case constant2:
// statements
break;
.
.
.
default:
// default statements
}
The previous example can be converted to a switch-case construct in the following way:
#include <stdio.h>
int main()
{
int n;
printf("Enter any number: ");
scanf("%d", &n);
switch(n)
{
case 1:
printf("January");
break;
case 2:
printf("February");
break;
case 3:
printf("March");
break;
case 4:
printf("April");
break;
case 5:
printf("May");
break;
case 6:
printf("June");
break;
case 7:
printf("July");
break;
case 8:
printf("August");
break;
case 9:
printf("September");
break;
case 10:
printf("October");
break;
case 11:
printf("November");
break;
case 12:
printf("December");
break;
default: printf("Invalid Entry!");
}
return 0;
} //main
Iteration
Iteration or repetition involves repeating a set of steps as long as a specified condition is true. Iteration is
expressed in the form of for loops and while loops. There are two types of iteration:
1. Bounded Iteration
Bounded iteration is iteration where the number of times has a determined maximum when it is started. This
is typically (but not necessarily) the case in a for loop.
for loops
For example: Write a program in C that adds the first 10 whole numbers and prints the sum.
#include <stdio.h>
int main()
{
int sum=0;
for(int i=1; i<=10; i++){
sum = sum + i;
} //for
This represents bounded iteration because at the start of the loop we can tell the loop will execute 10 times
(starting from 1 and ending at 10 inclusive).
while loops
It is possible to convert any for loop to a while because the components (initial_value, condition, increment) are
the same, but the order in which they occur is different.
initial_value;
while (condition){
statement(s);
increment;
}
For example: The for loop above can be converted to a while loop thus:
#include <stdio.h>
int main()
{
int sum=0;
int i = 1;
while (i<=10){
sum = sum + i;
i++;
} //for
2. Unbounded Iteration
Unbounded iteration is iteration that does not have a fixed maximum value upon the start of the loop. This is
often (but not always) the case for while loops.
For example: Write a program that prompts the user to enter a number. The program prints “Wrong!”
unless the user enters -1;
int main()
{
int n;
printf("Enter any number:");
scanf("%d", &n);
while (n!=-1){
printf("Wrong!");
printf("Enter any number:");
scanf("%d", &n);
} //for
printf("YEY");
return 0;
} //main
The above code continues to print “Wrong!” until the user enters -1. We cannot predict how many times the
loop will run because it depends on user input and is thus, unbounded.
For example: This following program will continuously print the statement inside the loop because there is no
condition or incrementation that will allow the program to exit the loop.
#include <stdio.h>
int main()
{
while (true)
{
printf("This is an infinite loop");
}
return 0;
} //main
OPERATORS IN C
Arithmetic Operators
The following table shows all the arithmetic operators supported by the C language. Assume variable A holds
10 and variable B holds 20 then −
Relational Operators
The following table shows all the relational operators supported by C. Assume variable A holds 10 and
variable B holds 20 then –
Logical Operators
Following table shows all the logical operators supported by C language. Assume variable A holds 1 and
variable B holds 0, then –