Control Structures in Programming Languages
Control Structures are just a way to specify flow of control in programs. Any algorithm or program can be
more clear and understood if they use self-contained modules called as logic or control structures. It
basically analyzes and chooses in which direction a program flows based on certain parameters or
conditions. There are three basic types of logic, or flow of control, known as:
• Sequence logic, or sequential flow
• Selection logic, or conditional flow
• Iteration logic, or repetitive flow
1.Sequential Logic (Sequential Flow)
Sequential logic as the name suggests follows a serial or sequential flow in which the flow depends on the
series of instructions given to the computer. Unless new instructions are given, the modules are executed in
the obvious sequence. The sequences may be given, by means of numbered steps explicitly. Also, implicitly
follows the order in which modules are written. Most of the processing, even some complex problems, will
generally follow this elementary flow pattern.
2.Selection Logic (Conditional Flow)
Selection Logic simply involves a number of conditions or parameters which decides one out of
several written modules. The structures which use these type of logic are known as Conditional
Structures. These structures can be of three types:
• Single AlternativeThis structure has the form:
If (condition) then:
[Module A]
[End of If structure]
Syntax:
if(condition)
{
// Statements to
execute if
// condition is true
}
Working of if statement
1. Control falls into the if block.
2. The flow jumps to Condition.
3. Condition is tested.
• If Condition yields true, goto Step 4.
• If Condition yields false, goto Step 5.
4. The if-block or the body inside the if is executed.
5. Flow steps out of the if block.
Note: If we do not provide the curly braces ‘{‘ and ‘}’ after if( condition ) then by default if statement will
consider the immediate one statement to be inside its block. For example,
if(condition)
statement1;
statement2;
// Here if the condition is true,
// if block will consider only statement1
// to be inside its block.
// C program to illustrate If statement // C program to illustrate If statement
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
int i = 10; int i = 10;
if (i < 15) { if (i > 15) {
printf("10 is less than 15 \n"); printf("10 is greater than 15 \n");
} }
printf("I am Not in if"); printf("I am Not in if");
} }
Output: Output:
10 is less than 15 I am Not in if
I am Not in if
• Multiple AlternativesThis structure has the form:
If (condition A), then:
[Module A]
Else if (condition B), then:
[Module B]
..
..
Else if (condition N), then:
[Module N]
[End If structure]
In C/C++ if-else-if ladder helps user decide from
among multiple options. The C/C++ if statements are
executed from the top down. As soon as one of the
conditions controlling the if is true, the statement
associated with that if is executed, and the rest of the
C else-if ladder is bypassed. If none of the conditions
is true, then the final else statement will be executed.
Syntax:
if (condition)
statement 1;
else if (condition)
statement 2;
.
.
else
statement;
// C program to illustrate nested-if statement
#include <stdio.h>
int main()
{
int i = 20;
// Check if i is 10
if (i == 10)
printf("i is 10");
// Since i is not 10
// Check if i is 15
else if (i == 15)
printf("i is 15");
// Since i is not 15
// Check if i is 20
else if (i == 20)
printf("i is 20");
// If none of the above conditions is true
// Then execute the else statement
else
printf("i is not present");
return 0;
}
Output:
i is 20
// C program to illustrate nested-if statement
#include <stdio.h>
int main()
{
int i = 25;
// Check if i is between 0 and 10
if (i >= 0 && i <= 10)
printf("i is between 0 and 10");
// Since i is not between 0 and 10
// Check if i is between 11 and 15
else if (i >= 11 && i <= 15)
printf("i is between 11 and 15");
// Since i is not between 11 and 15
// Check if i is between 16 and 20
else if (i >= 16 && i <= 20)
printf("i is between 16 and 20");
// Since i is not between 0 and 20
// It means i is greater than 20
else
printf("i is greater than 20");
}
Output:
i is greater than 20