Iteration Statements

Iteration Statements

The iteration statements allow a set of instructions to be performed repeatedly until a certain condition is fulfilled. The iteration statements are also called loops or looping statements. C++ provides three kinds of loops : for loop, while loop, and do-while loop.

All loop constructs of C++ repeat a set of statements as long as a specified condition remains true. This specified condition is generally referred to as a loop control. For all three loop statements, a true condition is any nonzero value. A zero value indicates a false condition.


Elements That Controls a Loop (part of a loop)

Every loop has its elements that control and govern its execution. Generally, a loop has four elements that have different purposes. These elements are as given below:
                                         Elements That Controls a Loop



1. Initialization Expression(s). 

Before entering in a loop, its control variable(s) must be initialized. The initialization of the control variable(s) give(s) the loop variable(s) their first value(s). The initialization expression(s) is executed only once, in the beginning of the loop.

2. Test Expression 

The test expression is an expression whose truth values decides whether the loop-body will be executed or not. If the test expression evaluates to true i.e., 1, the loop-body gets executed, otherwise the loop is terminated.
In an entry-controlled loop, the test-expression is evaluated before entering into a loop whereas in a exit-controlled loop, the test-expression is evaluated before exiting from the loop. In C++, the for loop and while loop are entry-controlled loops and do-while loop is exit-controlled loop.

3. Update expression(s) 

The update expression(s) change the value(s) of loop variable(s). The update expression(s) is executed ; at the end of the loop after the loop-body is executed.


4. The Body-of-the-Loop

The statements that are executed repeatedly (as long as the test-expression is nonzero) from the body of the loop. (n an entry-controlled loop, first the test expression is evaluated and if it is non zero, the body-of-the-loop is executed; if the test-expression evaluates to be zero, the loop is terminated. (n an exit-controlled loop, the body-of-the-loop is executed first and then the test-expression is evaluated. If it evaluates to be zero, the loop is terminated, otherwise repeated.


You may also like to visit:-


Post a Comment

0 Comments