Friday, 28 March 2014

Do While Loop in C

C Programming










The last of the three loops in C is the Do While loop, which is very similar to the While loop-the difference is that in the do loop the test condition is evaluated after the loop is executed rather than before. Here is the program which prints the numbers from 0 to 9.


Program
void main ( )
{
int num=0; //initialization
do 
{
printf("Number is %d\n",num); // statements to be repeated
num++; // increment
}
while(num<10) // test
}



Output
Number is 0
Number is 1
Number is 2
Number is 3
Number is 4
Number is 5
Number is 6
Number is 7
Number is 8
Number is 9


The do loop, unlike the other loops, has two keywords: do and while. The do keyword marks the beginning of the loop; the while keyword marks the end of the loop 

in do loop, the body of the loop is first executed, the n the test condition is checked. if the test condition is true, the loop is repeated, if it is false , the loop terminates. the important point is that the body of the loop will execute at least once.


No comments:

Post a Comment