You are on page 1of 11

C do while Loop Statement

Introduction
do-while loop is also an Iteration Statement in c
programming language.
do-while loop is an Exit Controlled loop because in dowhile loop the loop condition is checked at the end of the
loop and do-statement will execute at least once though the
condition is FALSE.
Thus, do-while loop is always executed at least once though
the condition is FALSE.

Syntax
do
{
statement 1;
statement2;
statement n;
}
while (loop condition) ;

Important Note:
The do-while loop must ends with a semicolon (;)
which is the important distinction from the ifstatement and while statement.
The do-while will continue to run until the loop
condition specified in while statement becomes
FALSE(0).

Do while loop Animation

http://www.cprogrammingexpert.com/C/Tutorial/control_statements/do_while_loop.aspx

Do while loop flow chart

Program
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int i=1;
do{
printf("\n%d",i);
i++;
} while(i<=10);
getch();
return 0;
}

Program
#include<stdio.h>
#include<conio.h>
int main(void) {
clrscr();
int i,j,total;
total = 0;
do {
printf("Enter a number (0 to stop):\n ");
scanf("%d", &i);
printf("Enter the number again:\n ");
scanf("%d", &j);
if(i != j) {
printf("Number Mismatch\n");
continue;
}
total = total + i;
} while(i);
printf("Total Iterations are %d\n", total);
getch();
return 0;
}

Program
#include<stdio.h>
main( )
{
do
{
printf ( "Hello World \n") ;
} while ( 2 < 1 ) ;
getch();
}

Program

#include <stdio.h>
void main()
{
int x = 5;
int i = 0;
// using do while loop statement
do
{
i++;
printf("%d\n",i);
}while(i < x);
}

Output
1
2
3
4
5

You might also like