For While and Do While Loops in C++

Iteration means Repetition. so, As the meaning suggests, Iteration construct means repetition of a set of statements depending upon a condition test. The statements are repeated again and again, till the condition is true. But once the condition false, the repetition stops.

C++ provides three kinds of loops –

  • For Loop
  • While Loop
  • Do while Loop

For while and do while loops, repeat a set of instructions as long as the condition remains true. Once false, the execution of loop will get stopped.
⇒ The condition is generally referred as Loop Control
⇒ A True Condition 
is any non-zero value
⇒ and a False Condition is a zero value.

Elements of a Loop –

Elements of loop(for while and do while loops)

1. Initialization Expression(s) –

The variables which controls the loop are called control variables. The control variables of loops must be initialized before entering in the loop and these variables are placed into initialization expression of the loop. The initialization expression executed only once at the beginning of the loop

2. Test Expression/Control Statement –

A test expression is a expression which decides whether the loop body will be executed or not. If the test expression results false, then the body of the loop will not be executed, otherwise, body of loop will be executed.

Depending on the position of the control expression in the loop, a control structure can be classified into two types –

Entry Controlled Loop –

If the test expression is placed before the body of loop, and is evaluated before entering into the loop. In entry Controlled loop, first the test expression is evaluated. If the test expression results a non-zero value, the body of the loop will be executed. If it results a value of zero, then body of loop will not be executed.
The for loop and while loop are entry controlled loop.

Exit Controlled Loop –

If the test expression is placed after the body of loop, and is evaluated before exiting from the loop. In exit controlled loop, first the body of the loop will be executed and then test expression is evaluated. The body of the loop will be executed, if the test expression results a non-zero value. If it results a value of zero, then body of loop will not be executed.
The do-while loop is an exit controlled loop.

3. Update Expression-

The update expression changes the value of control variable.. The update expression is executed at the end of the loop after the loop-body is executed.

4. The Body of the Loop –

The Body of the Loop is formed from the statements, that are executed repeatedly.
The for loop and while loop will not get executed even once if the test expression gets failed.
But the do while loop will be executed atleast once even the test expression fails at the beginning.

Entry and exit controlled loop(For While and Do While Loops in C++)

For Loop –

For loop is very simple and easier to understand. All the loop elements are gathered in one place in for loop.

Syntax of for Loop :
for(initialization expression(s); test expression(s); update expression(s))
    {
    Body of the Loop ;
    }
Working of For Loop :
  1.  Firstly, the initialization expression(which mainly contains control variables) will be executed only once at the beginning of the for loop.
  2. Then test expression will be evaluated.
  3. If the test expression results a false value, the loop gets terminated.
  4. If the test expression results a true value, then the body of the loop will be executed.
  5. Then update expression updates the control variables value and the test expression will be evaluated again.
  6. If test expression results a true value, repeat step 4. otherwise step 3.
Flow Chart of For Loop :

for Loop (For While and Do While Loops in C++)

Example Of C++ Program of for Loop – Click Here

     

Incoming search terms:

  • flow diagram of loop statement entry control

Leave a Reply