Cpp Control Flow Statements 15

Loops

Loops help us to execute a specific number of statements a specified number of times. The C++ language provides 2 types of loops.

(i). Entry control loops (for loop and while loop)
(ii). Exit control loops (do while loop)

These statements are also known as iteration or looping statements.

Entry control loops

Entry control loops are the loops in which the condition is checked first and then the control enters the loop.

In “C++” language we have two entry control loops.

(i) for loop
(ii) while loop

For loop

for loop is an entry control loop. In for loop we are required to pass three arguments.

Syntax:

(i).
for(A;B;C)
single Statement;

(ii).
for(A;B;C)
{
Statements;
Statements;
Statements;
}

A : initialization
B : Test condition
C : Increment / decrement

for(initialization ;test condition ;increment/decrement)
{
Statements;
Statements;
Statements;
}

Note: all the argument passed in for loops are optional.

For loop can take following forms

1. for(a;b;c)
2. for(;b;c)
3. for(a;;c)
4. for(a;b;)
5. for(a;;)
6. for(;b;)
7. for(;;c)
8. for(;;)