THE WHILE STATEMENT
while (test condition)
{
body of the loop
}
The while is an entry-controlled loop statement. The test-condition is evaluated and if the condition is true, then the body of the loop is executed. After execution of the body, the test-condition is once again evaluated and if it is true, the body is executed once again. This process of repeated execution of the body continues until the test-condition finally becomes false and the control is transferred out of the loop. On exit the program continues with the statement immediately after the body of the loop.
THE DO STATEMENT
do
{
body of the loop
}
while (test-condition);
The di statement is an exit-controlled loop. On reaching the do statement, the program proceeds to evaluate the body loop first. At the end of the loop, the test-condition in the while statement is evaluated. If the condition is true, the program continues to evaluate the body of the loop once again. This process continues as long as the condition is true. When the condition become false, the loop will be terminated and the control goes to the statement that appears immediately after the while statement. As it is a exit controlled loop and therefore the body of the loop is always executed at least once.
THE FOR STATEMENT
The for loop is another entry-controlled loop that provides a more consise loop control structure. The general form of the for loop is
for (initialization)
{
body of the loop
}
The execution of the for statement is as follows:
1.Initialization of the control variables is done first, using assignment statements such as i=1 and count = 0. The varianles i and count are known as loop-controlled variables.
2.The value of the control variable is tested using the test-condition. The test-condition is a relational expression, such as i<10 that determines when the loop will exit. If the condition is true, the body of the loop is executed; otherwise the loop is terminated and the execution continues with the statement that immediately follows the loop.
3. When the body of the loop is executed, the control is transferred back to the for statement after evaluating the last statement in the loop. Now the control variable is incremented using an assignment statement such as i=i+1 and the new value of the control variable is again tested to see whether is satisfies the loop condition. if the condition is satisfied, the body of the loop is again executed. This process continues till the value of the control variable fails to satisfy the test-condition.
Break Statement
When a break statement is encountered inside a loop, the loop is immediately exited and the program continues with the statement immediately following the loop. When the loop are nested, the break only exit from the loop containing it. That is, the break exit only a single loop.
Continue Statement
The continue statement tells the compiler, SKIP THE FOLLOWING STATEMENTS AND CONTINUE WITH THE NEXT ITERATION its systax is simply continue;
Wednesday, June 9, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment