do while loop in c

do while loop in c

The do-while loop statement is also used for looping as for loop. The syntax of do-while syntax

do
{
statement
statement
statement
……..
……..
}while(expression);

note in the do-while loop semi colon is placed after the parenthesis containing the expression

do_whileloop

in the do-while loop semi colon is place after the parenthesis containing the expression. Here in this example of do while loop first the body of the loop is execute and after expression is evaluated. if the expression is true then the body of the loop is executed and this process is executes until the expression is not false. When in the loop expression is false the loop terminates.

Generally the while loop is use more frequently than the do-while loop. But there are many situations the do-while loop is better to check the condition at the bottom of loop or check the condition after the body execute. For example in the next program understand the concept of do-while loop

/* program to count digit in a number*/
#include<stdio.h>
int main()
{
int num,count=0,rem;
printf("Enter a number");
scanf("%d",&num);
do
{
num=num/10;
count++;
}while(num>0);
printf("the number of digits are =%d",count);
return 0;
}

#include<stdio.h>
int main ()
{
/* local variable definition */
int a = 15;
/* do loop execution */
do
{
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );
return 0;
}

value of a: 17
value of a: 18
value of a: 19