The normal flow of execution in a high level language is sequential, i.e., each statement is executed in the order of its appearance in the program. However, depending on the requirements of a problem it might be required to alter the normal sequence of execution in a program. The statements which specify the order of execution of statements are called control flow statements.

Statements

Statements are the instructions given to the computer to perform any kind of action. Action may be in the form of data movement, decision making, etc. Statements form the smallest executable unit within a C++ program. Statements are always terminated by semicolon.

Compound Statement

A compound statement is a grouping of statements in which each individual statement ends with a semi-colon. The group of statements is called block. Compound
statements are enclosed between the pair of braces ( { } ). The opening brace ( { ) signifies the beginning and closing brace ( } ) signifies the end of the block.

Conditional Statements

Sometimes the program needs to be executed depending upon a particular condition. C++ provides the following statements for implementing the selection control structure. 

  • ‘if ’ statement
  •  ‘if else’ statement
  •  ‘nested if’ statement
  • ‘switch’ statement

‘if’ statement

Syntax of the ‘if’ statement:

if (condition)
{
statement(s);
}

If the 'if condition' is true, statement 1 is executed; otherwise it is skipped. The statement 1 may either be a single or compound statement.

‘if else’ statement

Syntax of the if-else statement:

if (condition)
statement1;
else
statement2;

switch statement

The if and if-else statements permit two way branching whereas switch statement permits multiple branching. The syntax of switch statement is:

switch (var / expression)
{
case constant1 : statement1;
| | |
break;
case constant2 : statement2;
| | |
break;
default : statement3;
}

The execution of switch statement begins with the evaluation of expression. If the values of expression matches with the constant then the statements following this statement execute sequentially till it executes break. The break statement transfers control to the end of the switch statement. If the value of expression does not match with any constant, the statement with default is executed.

Loop Construct

It is also called a Repetitive control structure. Sometimes you require a set of statements to be executed a number of times by changing the value of one or more variables each time to obtain a different result. This type of program execution is called looping. C++ provides the following constructs:

  • while loop
  • do - while loop
  • for loop 

While loop

Syntax of while loop:

while (condition)
{
statement(s);
}

A condition (loop control variable) is first evaluated. If the condition is true, the loop body is executed and the condition is re-evaluated. Hence, the loop body is executed repeatedly as long as the condition remains true. Normally, the three operations listed below must be performed on the loop control variable.

  1. Initialize the loop control variable
  2. Test the loop control variable
  3. Update the loop control variable

Unless the loop control variable is updated in the loop body, its value cannot change and loop exit will never occur.

do-while loop

Syntax of do-while loop:

do
{
} while (condition);

After each execution of the loop body, the condition is true, the loop body is executed again. If the condition evaluates to false, loop exit occurs and the next program statement is executed. The loop body is always executed at least once. 

One important difference between the while loop and the do-while loop is the relative ordering of the conditional test and loop body execution. In the while loop, the loop repetition test is performed before each execution of the loop body; the loop body is not executed at all if the initial test fails. In the do-while loop, the loop termination test is Performed after each execution of the loop body; hence, the loop body is always executed at least once.

For loop

It is a count controlled loop in the sense that the program knows in advance how many times the loop is to be executed.

Syntax of for loop:

for (initialization; decision; increment/decrement)
{
statement(s);
}

In for loop, three operations take place:

  1. Initialization of loop control variable
  2. Testing of loop control variable
  3. Update the loop control variable either by incrementing or decrementing

Jump Statements

The jump statements unconditionally transfer program control within a function.

  • goto statement
  • break statement
  • continue statement

goto Statement

Syntax of goto statement:

goto pgr;
| | |
pgr :

pgr is known as label. It is a user defined identifier. After the execution of goto statement, the control transfers to the line after label pgr.

It is not a good programming to use goto statement in a program.

break Statement

The break statement can be used in a switch statement and in any of the loops. It causes program execution to pass to the next statement following the switch or the loop.

Syntax of break statement:

while (condition)
{
statement 1;
if (condition)
break ;
statement 2;
}
statement 3;

The break statement skips rest of the loop and goes out of the loop.

continue Statement

The continue statement is used in loops and causes a program to skip the rest of the body of the loop.

while (condition)
{
statement 1;
If (condition)
continue ;
statement 2;
}
statement 3;

The continue statement skips rest of the loop body and starts a new iteration.

exit ( ) function

The execution of a program can be stopped at any point with exit ( ) and a status code can be informed to the calling program. The general format is:

exit (code) ;

where code is an integer value. The code has a value 0 for correct execution. The value of the code varies depending upon the operating system. It requires a process.h header file.