Easy Tutorial
❮ C Macro Assert C Goto Statement ❯

C do...while Loop

C Loops

Unlike the for and while loops, which test the loop condition at the beginning, the do...while loop in C checks its condition at the end of the loop.

The do...while loop is similar to the while loop, but the do...while loop ensures that the loop is executed at least once.

Syntax

The syntax for the do...while loop in C:

do
{
   statement(s);

} while( condition );

Note that the condition expression appears at the end of the loop, so the statement(s) within the loop are executed at least once before the condition is tested.

If the condition is true, the control flow jumps back to the do statement, and the statement(s) within the loop are executed again. This process repeats until the given condition becomes false.

Flowchart

Example

Example

#include <stdio.h>

int main ()
{
   /* Local variable definition */
   int a = 10;

   /* do loop execution, executed at least once before the condition is tested */
   do
   {
       printf("Value of a: %d\n", a);
       a = a + 1;
   } while( a < 20 );

   return 0;
}

When the above code is compiled and executed, it produces the following result:

Value of a: 10
Value of a: 11
Value of a: 12
Value of a: 13
Value of a: 14
Value of a: 15
Value of a: 16
Value of a: 17
Value of a: 18
Value of a: 19

C Loops

❮ C Macro Assert C Goto Statement ❯