do while loop in c program

Do while Loop in C  Progrming :

The while loop consturct that we have discussed in the previous section , makes a test of condition before the loop is  executed . therefore , the body of the loop may not be excuted at all if the conditon is not satisfied at the very first attempt . On some occasions it might  be necessary to execute the body of the loop before the test is preformed . such situations can be handled with the helpe of the do statement . this takes the from

                               
                            initialization ;
                            do
                            {

                              body of the loop
                               increment /  decerment ;

                               }
                            while (test-condition);




How do while loop words :
do while loop woeks in two step .

  • Initially program control transfers to body of loop . It executes all statements inside loop body transfers control to loop condition .
  • loop condition containd set of relationl and logical expressions . If conditional experssion evaluates 1 (true ) then loop repeats again otherwise if conditonal expression evaluates 0 (false) loop terminates.                                                                                                                                                                                                                                                                                            the above two steps are repeated unitil loop condition is met . 









Flowchart of do  while loop  :



Example do while loop :

Let us write a C program to print natural numbers from 1 to 10 using do...while loop.

code :

#include<stdio.h>
int main()
{
    /* initialization */

    int i;
    i=1;
    do
    {
        /* body of loop */
        printf("%d ",i);
        /* increment */
        i++;
    }
    /* loop condition */
    while (i<=5);



output :

    1 2 3 4 5














No comments

Powered by Blogger.