Source One Educational Society    
Home | OS Install | MS-Office | Photoshop | SQL | Networking | HTML | Java | Model Papers | FAQ's | Health Tips | News | Jobs | Fun SMS
C - Tutorial
INTRODUCTION
o Getting Started
o C Boot Disk
o What Is An Identifier
o Getting Started C
PROGRAM CONTROL
o While Loop
o Do-While Loop
o For Loop
o If Statement
o Break And Continue
o Switch Statement
o Goto Statement
o Finally, A Meaningful Program
ASSIGNMENT & LOGICAL COMPARES
o Integer Assignment Statements
o Additional Data Types
   
   
 
 
C Language Tutorial
Program Control  

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);

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.

 
 
 
Quick Links
Java FAQ's
XML FAQ's
.Net FAQ's
Javascript FAQ's
SQL Interview Questions
 
 
 
 
Home  |  Aboutus  |  Post Your Content  |  Tell a Friend  |  Contact us  |  Sitemap
© 2009 365edu. All Rights Reserved. Site by 365edu. Best view in 1024x768 resolution.