Control statements in both C and C++ specify the flow of program execution and which instructions of the program must be executed next. Thus allowing programmer to make decisions, perform tasks properly. It important for programmer to know how a program execution flows, for better understanding and fast debugging.
C/C++ provides four different styles of flow control:
- Sequence control
- Selection or Decision making statements
- Repetition or iteration control
- Jump statement
Sequence Control
This one is default mode execute code statements one line after another. Statements executed in a specified order and no statement is executed more than once.
// Example of Sequence control flow
int main(){
int i=5;
printf("Square of i = %d",i);
return 0;
}
Output:
Square of i = 25
Selection or Decision making control statements
This statement has branches to follow depending on condition executed. Following are the decision making statements used in C/C++.
if statement
if takes an expression in parenthesis and a block of statements follow which executed only if the condition inside parenthesis is true. Otherwise these block is skipped and note that expression will be assumed to be true if its evaluated values is non-zero.
Syntax:
if (expression) statement; or if (expression) { Block of statements; }
Illustration of IF statement
int main()
{
int a = 1, b = 3;
if(a > b)
printf("a greater than b\n");
// Parenthesis optional if IF block contain only one statement
printf("Above block is ignored\n\n");
if(a<b){
printf("a less than b\n");
printf("Two statements inside IF block\n");
}
return 0;
}
Output:
Above block is ignored a less than b Two statements inside IF block
if-else Statement
In addition to above if block else block added which execute only when IF block expression is false.
Syntax:
if (expression) statement; else statement;
Illustration of IF-ELSE control statement
int main()
{
int a = 1, b = 3;
if(a > b)
printf("a greater than b\n");
else
printf("a less than b");
// Parenthesis optional if ELSE block contain only one statement
return 0;
}
Output:
a less than b
Ternary Operator
The ? : operator works just like an if-else statement except it is an operator that can be used within expressions.
Syntax:
if condition is true ? then X return value : otherwise Y value;
Illustration of Ternary operator
int a = 1, b = 3;
a > b ? printf("a greater than b\n"): printf("a less than b");
Output:
a less than b
Nested if-else statement:
Using if-else block inside other if-else block is called Nested if-else. It is completely legal to use if-else inside another. This statement has ability to control program flow based on multiple levels of condition.
Syntax:
if (exp1)
{
//executed when exp1 is true
if (exp2)
{
//executed when exp2 is true
}
else
{
//executed when exp2 is false
}
}
Illustration of Nested IF-ELSE statement
int a = 4, b = 3;
if(a>=b) {
if(a==b)
printf("a equal to b\n");
else
printf("a greater than b");
}
else
printf("b greater than a");
Output:
a greater than b
Else-if ladder
Used when more than one condition need to be evaluated.
Syntax:
if (condition) statement; else if (condition) statement; . . else statement;
Illustration of Else-if Ladder
int main()
{
int a = 10;
if (i < 10)
cout<<"a less than 10";
else if (i > 10)
cout<<"a greater than 10";
else if (i == 10)
cout<<"a equal to 10";
else
cout<<"a not found";
return 0;
}
Output:
a equal to 10
switch statement:
The switch statement is much like a nested if – else statement executes one or more of a series of cases, based on the value of an expression. The switch expression is evaluated and integral promotions applied than compared with the constant expressions in the case labels.
Syntax:
switch(expression) { case constant-expression1: statements1; case constant-expression2: statements2; case constant-expression3: statements3; default : statements4; }
Some features of switch statement:
- The constant expression must be of an integral type
- No two case label can be same.
- There is no limit on the number of case labels in switch statement.
- Only one default statement can present.
- The cases and default labels can occur in any order, but it is common practice to put default statement after the cases.
- Break Keyword: There should be a break statement between cases to prevent execution of other case statements and terminate switch block.
- If the switch expression value does not match any case label than the default statement is executed and if there is no default label, then execution of the switch statement terminates.
Switch statement Example
int main()
{
int num = 2;
switch (num)
{
case 1: printf("I am Number 1");
break;
case 2: printf("I am Number 2");
break;
case 3: printf("I am Number 3");
break;
default: printf("No match");
break;
}
return 0;
}
Output:
I am Number 2
Iteration Control Statements
Iteration control is a process where a set of instructions or block of statements repeated in a specified number of times or until a certain condition became false.
Loops are classified into three major in C/C++ and 4th only in C++.
- while loop
- do-while loop
- for loop
- Nested loop
while loop
The while loop statement allows the programmer to specify that a program should repeat set of instruction inside the block till the condition remains true. While is the most basic loop in C/C++. While loop is Entry Controlled loop.
Basic syntax of while loop:
while ( condition ) { Block of statements; }
Example of while loop
int i=0;
while(i<5){
cout<<i<<" ";
i++;
}
Output:
0 1 2 3 4
do – while loop
The do – while loop is similar to the while statement. In do – while loop, the test condition is executed after the execution of loop body. So, irrespective of what the test condition is this loop get executed atleast once. Do-While loop is Exit Controlled loop.
Basic syntax of do…while loop:
do { Block of statements; } while(expression);
DO-WHILE Example
int i=0;
do{
cout<<i<<" ";
i++;
}while(i<5);
Output:
0 1 2 3 4
for loop
for loop is almost similar to while loop. The for loop statement allows the programmer to specify that a program should repeat set of instruction inside the block till specific number of times the condition remains true. For loop is Entry Controlled loop.
Basic syntax of for loop:
for (initializing variable; test-condition; increment/decrement) { Block of statements; }
FOR LOOP example
for(int i=0;i<5;i++)
{
cout<<i<<" ";
}
Output:
0 1 2 3 4
Nested Loop statement
Using loop inside other loop is called Nested Loop. It is completely legal to use Loop inside another. This statement has ability to control program flow based on multiple levels of condition.
Basic syntax
for (initializing variable; test-condition; increment/decrement) { for (var; test-condition; increment/decrement) { Block of statements; } statements; }
for (int i = 1; i <= 5; ++i) {
printf("Outer loop iteration %d \n",i);
for (int j = 1; j <=2; ++j) {
printf("i = %d; j = %d\n",i,j);
}
}
Output:
Outer loop iteration 1 i = 1; j = 1 i = 1; j = 2 Outer loop iteration 2 i = 2; j = 1 i = 2; j = 2 Outer loop iteration 3 i = 3; j = 1 i = 3; j = 2 Outer loop iteration 4 i = 4; j = 1 i = 4; j = 2 Outer loop iteration 5 i = 5; j = 1 i = 5; j = 2
Extra’s
Based on initialization, execution, flow and exit, the above loops further grouped or classified into 4 different type namely.
Counter Control Statements or loops
A count-controlled loop will exit after running a certain known number of times called an index or counter. When the index/counter reach a certain value the loop will end. Counter controlled repetition is often called definite iteration because the number of repetitions is known before the loop begins executing.
// using for loop as counter controlled
for(int i=0;i<5;i++)
{
cout<<i<<" ";
}
// using while loop as counter controlled
int i=0;
while(i<5){
cout<<i<<" ";
i++;
}
// using do-while loop as counter controlled
int i=0;
do{
cout<<i<<" ";
i++;
}while(i<5)
Sentinel Control Statements or loops
In Sentinel controlled loops, we don’t know exactly know how many times loop will be executed. Sentinel-controlled loop also known as indefinite repetition.
//Example
int num;
do{
printf("Input a number.\n");
scanf("%d", &num);
}
while(num > 0);
Output:
Input a number. 1 Input a number. 2 Input a number. 3 Input a number. 5 Input a number. -1