> ~ Online tutorial

The Do-While Loop

Avariation of the while loop is illustrated in the program dowhile.c, which you should load
and display.
/* This is an example of a do-while loop */
main( )
int i;
i = 0;
do {
printf("the value of i is now %d\n",i);
Program Control C Tutorial 3-1
i = i + 1;
} while (i < 5);
This program is nearly identical to the last one except that the loop begins with the reserved word "do", followed by a compound statement in braces, then the reserved word "while", and finally an expression in parentheses. The statements in the braces are executed repeatedly as long as the expression in parentheses is true. When the expression in parentheses becomes false, execution is terminated, and control passes to the statements following this statement. Several things must be pointed out regarding this statement. Since the test is done at the end of the loop, the statements in the braces will always be executed at least once. Secondly, if "i", were not changed within the loop, the loop would never terminate, and hence the program would never terminate. Finally, just like the while loop, if only one statement will be executed within the loop, no braces are required. Compile and run this program to see if it does what you think it should do. It should come as no surprise to you that these loops can be nested. That is, one loop can be included within the compound statement of another loop, and the nesting level has no limit.

Please Give Us Your 1 Minute In Sharing This Post!
Please Give Us Your 1 Minute In Sharing This Post!
Powered By: BloggerYard.Com