C - Do While loop


1. When a loop is constructed using while statement,the test for continuation of the loop is carried out at the beginning of each pass. Sometimes, however it is desired to have a loop with the test for continuation at the end of each pass. This can be accomplished with the help of the do-while statement.

2. The general form of do-while statement is do statement while(expression) The included statement will be executed repeatedly , as long as the value of expression is not zero .

3. The statement will always be executed atleast once, since the test for repetition does not occur until the end of the first pass through the loop.

4. The statement can be either simple or compound, through most application will require to be compound statement. It must include some feature which eventually alter the expression so that the looping action can terminate.


Consider an example


#include<stdio.h>
void main() {
	char a='A';
   do{
    printf("%c\n",a);
    a++;
   }
   while(a!='H');
}

Output:


A
B
C
D 
E
F 
G


  • Print Page