do-while Statement in C



Syntax:
do{
/*block of statement*/
}while(condition);

Explanation:
Here, while and do are the keywords which is know to the compiler.
Condition can be any expression
This is very similar to while loop.
Here, the block of statement enclosed in the opening and closing braces after the keyword do is executed at least once. After the execution of the block of statement for the first time, the condition in the while is executed.
If the condition is satisfied then the block of statement inside the do block is executed. After the execution of block of statement, again condition is check. If the conditional expression returns true, the block of statement is executed again. This process is followed till the conditional expression returns false value.
Note:
It is necessary to write semicolon (;) after the while(condition) as shown in the syntax else you will get an error.

The above example for while loop can re-written as follows:
#include<stdio.h>
#include<conio.h>
void main(){
int i=0;
clrscr();
do{
i=i+1;
printf("%d This will be repeated 5 times\n", i);
}while(i!=5);
printf("End of the program");
getch();
}
Output:
1 This will be repeated 5 times
2 This will be repeated 5 times
3 This will be repeated 5 times
4 This will be repeated 5 times
5 This will be repeated 5 times
End of the program


Responses

0 Respones to "do-while Statement in C"

Post a Comment

 

Total Pageviews

Return to top of page Copyright © 2011 | Kuppam Engineering College Converted into Blogger Template by Mohan Murthy