do...while loop examples

« Previous

Try following example to understand do...while loop. You can put the following code into a test.c file and then compile it and then run it.

#include <stdio.h>
main()
{
    int i = 10;
    do{
       printf("NewFile. %d\n", i );
       i = i -1;
    }while ( i > 0 );
}
                                        

Output:

NewFile. 10
NewFile. 9
NewFile. 8
NewFile. 7
NewFile. 6
NewFile. 5
NewFile. 4
NewFile. 3
NewFile. 2
NewFile. 1
                            

You can make use of break to come out of do...while loop at any time.

#include <stdio.h>
main()
{
    int i = 10;
    do{
       printf("NewFile. %d\n", i );
       i = i -1;
       if( i == 6 )
       {
          break;
       }
    }while ( i > 0 );
}
                            

Output:

NewFile. 10
NewFile. 9
NewFile. 8
NewFile. 7
NewFile. 6
                            


« Previous
Have Any Suggestion? We Are Waiting To Hear from YOU!

Your Query was successfully sent!