C do…while loop

In this lesson, you will learn about the do…while loop in C, its usage, and examples to better understand the topic.

The do-while loop in C

The do-while loop works similarly to the while loop. The significant difference is that the loop executes first, then evaluates the condition. So the loop will run at least once, and the loop will iterate until the condition is false.

Syntax of do-while loop

do {
code to be processed here;
} while (condition is true);

“Do { … } while (…)” is the do … while the loop block code “condition” is the condition to be assessed by the loop “block code …” is the code performed at least once by the do … while the loop is performed at least once.

Flowchart of do-while loop

The flow chart displayed below demonstrates how the do-while… loop executes

Example of Do while loop in C

// Example of do While loop
#include <stdio.h>
int main ()
  int var, evennumber;
  printf ("C ***Do While loop*** Example\n");
  printf (" -----Do While loop ---------\n");
  printf ("The table of %d \n", 2);
  var = 1;
      evennumber = 2 * var;
      printf ("2 * %d is %d \n", var, evennumber);
  while (var <= 10);
  return 0;


C ***Do While loop*** Example
 -----Do While loop ---------
The table of 2 
2 * 1 is 2 
2 * 2 is 4 
2 * 3 is 6 
2 * 4 is 8 
2 * 5 is 10 
2 * 6 is 12 
2 * 7 is 14 
2 * 8 is 16 
2 * 9 is 18 
2 * 10 is 20

C While Vs. Do While Loop Statement

The while loop differs from the do-while loop. With a ‘while’ loop, the condition is evaluated at the beginning of each loop iteration; if the condition is false, the loop will never be executed.

On the other hand, the do-while loop will execute at least once, even if the initial condition is false because the loop executes its body before evaluating the condition.

In the next lesson, you will learn two important keywords in C, the Break and continue keywords.