Copyright © tutorialspoint.com

Latest C Tutorial

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("Hello %d\n", i );
       i = i -1;
    }while ( i > 0 );
}

This will produce following output:

Hello 10
Hello 9
Hello 8
Hello 7
Hello 6
Hello 5
Hello 4
Hello 3
Hello 2
Hello 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("Hello %d\n", i );
       i = i -1;
       if( i == 6 )
       {
          break;
       }
    }while ( i > 0 );
}

This will produce following output:

Hello 10
Hello 9
Hello 8
Hello 7
Hello 6

previous

Copyright © tutorialspoint.com