Do While

 

The condition or expression used in do-while is same as while loop except, the condition or expression is evaluated at the end of each iteration

 

Syntax-

Do

{

//statements;

} while(expression)

 

In this case, loop body statements are executed before evaluating the expression.

After each iteration, the expression is evaluated if it gives non-zero value, control is transferred to next iteration or else loop is terminated.

The loop statement is executed at least once if the expression gives zero value in first iteration.

 

 

Examples

 

Int x=1;

Do

{

printf(“  \n %d “,x);

x++;

}while(1) // infinite loop

 

 

int x = 5;

do

{

printf(“\n %d “,x);

x–;

}while(x) //infinite loop

 

 

int x = 5, y=1;

do

{

printf(“\n %d %d”, x,y);

x–;

}while(x+y) // infinite loop

 

 

Comparison between While & Do While Loop

 

While Loop

Do While Loop

-Expression is evaluated before starting new iteration.

-Expression is evaluated at the end of each iteration.

-If the expression gives zero result while starting first iteration, loop is terminated without executing loop body.

-If the expression gives zero result while starting first iteration, loop body is executed at least once.

-Syntax

-Syntax

-Example

-Example

 

 

 

 

Registration


A password will be e-mailed to you.

Feedback Form

Name (required)

Email (required)

Feedback