Pages

Saturday, February 27, 2010

THE do-while LOOP

THE do-while LOOP


Introduction

The do-while loop is used when you want to execute the loop body at least once. The do-while loop executes the loop body and then traces the condition.

Program/Example

The general format for a do-while loop is

do     simple or compound statement     while (condition)     For example, i = 0; do {     printf(" the value of i is %d\n", i);     i = i + 1; } while (i<5)>

Explanation

  1. The loop body is executed at least once.

  2. The condition is checked after executing the loop body once.

  3. If the condition is false then the loop is terminated.

  4. In this example, the last value of i is printed as 5.


No comments:

Post a Comment