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:
1. Sequence logic, or sequential flow
2. Selection logic, or conditional flow
3. Iteration logic, or repetitive flow
1, if statements in C++ Programming
If statement is a conditional statement which is used to make decision.
It is used when a single condition is to be checked.
A condition is enclosed in if statement which decides the sequence of
execution of instruction.
If the condition is true, the statements inside if statement are executed,
otherwise they are skipped.
Syntax
if (condition)
{
// Statements to execute if
// condition is true
}
Flowchart for if statement
Examples of if statement in C++
// C++ program to illustrate If statement
#include <iostream>
using namespace std;
int main()
int i = 10;
if (i < 15) {
cout << "10 is less than 15 \n";
cout << "I am Not in if";
Explanation:
Program starts.
i is initialized to 10.
If-condition is checked. 10 < 15, yields true.
“10 is less than 15” gets printed.
if condition yields false. “I am Not in if” is printed.
2, if … else statement
if … else statement is a two way branching statement.
It is similar to if statement but the only difference is if the condition is
false then a different block of statements are executed which is inside
else statement.
Syntax
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
Flowchart for if … else statement
Examples of if else statement in C++
// C++ program to illustrate if-else statement
#include <iostream>
using namespace std;
int main()
int i = 20;
// Check if i is 10
if (i == 10)
cout << "i is 10";
// Since is not 10
// Then execute the else statement
else
cout << "i is 20\n";
cout << "Outside if-else block";
return 0;
Explanation:
Program starts.
i is initialized to 20.
if-condition is checked. i == 10, yields false.
flow enters the else block.
“i is 20” is printed
“Outside if-else block” is printed.
Example 2:
// C++ program to illustrate if-else statement
#include <iostream>
using namespace std;
int main()
{
int i = 25;
if (i > 15)
cout << "i is greater than 15";
else
cout << "i is smaller than 15";
return 0;
3, if … else if … else statement
if … else if … else statement is used for multiple branching.
When there are two or more conditions that needs to be checked to
decide which block of statement is to be executed, it is used.
The number of else if statements depend upon the number of
conditions to be checked.
Syntax
if (condition)
statement 1;
else if (condition)
statement 2;
.
.
else
statement;
// C++ program to illustrate if-else-if ladder
#include <iostream>
using namespace std;
int main()
{
int i = 20;
// Check if i is 10
if (i == 10)
cout << "i is 10";
// Since i is not 10
// Check if i is 15
else if (i == 15)
cout << "i is 15";
// Since i is not 15
// Check if i is 20
else if (i == 20)
cout << "i is 20";
// If none of the above conditions is true
// Then execute the else statement
else
cout << "i is not present";
return 0;
}
Flowchart for if … else if … else statement
Examples of if else if the ladder
// C++ program to illustrate if-else-if ladder
#include <iostream>
using namespace std;
int main()
int i = 20;
// Check if i is 10
if (i == 10)
cout << "i is 10";
// Since i is not 10
// Check if i is 15
else if (i == 15)
cout << "i is 15";
// Since i is not 15
// Check if i is 20
else if (i == 20)
cout << "i is 20";
// If none of the above conditions is true
// Then execute the else statement
else
cout << "i is not present";
return 0;
}
Explanation:
Program starts.
i is initialized to 20.
condition 1 is checked. 20 == 10, yields false.
condition 2 is checked. 20 == 15, yields false.
condition 3 is checked. 20 == 20, yields true. “i is 20” gets printed.
“Outside if-else-if” gets printed.
Program ends.
Example 2:
Another example to illustrate the use of if else if ladder in C++.
// C++ program to illustrate if-else-if ladder
#include <iostream>
using namespace std;
int main()
int i = 25;
// Check if i is between 0 and 10
if (i >= 0 && i <= 10)
cout << "i is between 0 and 10" << endl;
// Since i is not between 0 and 10
// Check if i is between 11 and 15
else if (i >= 11 && i <= 15)
cout << "i is between 11 and 15" << endl;
// Since i is not between 11 and 15
// Check if i is between 16 and 20
else if (i >= 16 && i <= 20)
cout << "i is between 16 and 20" << endl;
// Since i is not between 0 and 20
// It means i is greater than 20
else
cout << "i is greater than 20" << endl;