Sunday, August 16, 2009

do-while and for loop

do-while loop

The do-while loop has the form:

} while( loop_condition );

the statementlist in the do-while loop is executed at least once, i.e. it is a post test loop. The basic behavior of do-while loop:

1. The statementlist is executed
2. The loop_condition is evaluated
3. If it is true the control returns to step 1, otherwise control passes to the first statement following the loop.

for loop

The for loop has the form:

for( init_expression; boolean_expression; step_expression )

When the program execution reaches a for statement:

1. init_expression is evaluated
2. boolean_expression is evaluated
3. if it is true
a. statementlist is executed
b. step_expression is evaluated
c. Control returns to step 2
4. Otherwise control passes to the statement following the for

The init_expression is evaluated only once at the beginning of execution. Note that it is possible that the statementlist is never executed.

Previous post

Next post

No comments:

Post a Comment