chapter9-loop T-2
chapter9-loop T-2
com
Flow of Control
Looping statement
while loop
do-while loop
for loop
While loop
The flow diagram indicates that a condition 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. As soon as the
condition becomes false, it comes out of the loop and goes to the statement next to
the ‘while’ loop.
do-while loop
Syntax of do-while loop
do
{
statements;
} while (condition);
Note : That the loop body is always executed at least once. One important
difference between the while loop and the do-while loop the relative ordering of
the conditional test and loop body execution. In the while loop, the loop
repetition test is performed before each execution the loop body; the loop body
is not executed at all if the initial test fail. 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 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.
The flow diagram indicates that in for loop three operations take place:
Operation (i) is used to initialize the value. On the other hand, operation (ii) is
used to test whether the condition is true or false. If the condition is true, the
program executes the body of the loop and then the value of loop control
variable is updated. Again it checks the condition and so on. If the condition is
false, it gets out of the loop.
Jump Statements
goto statement
break statement
continue statement