C Programming/C Loop Statement

C do–while Loop

Updated on January 9, 2026
1 min read

What is a do–while Loop?

do–while loop is a looping statement that executes the loop body at least once, and then checks the condition.

Key idea:

Condition is checked after execution, not before.

Why do–while is different from while

  • while loop
  •  → Condition checked before execution
  •  → Loop may run zero times
  • do–while loop
  •  → Condition checked after execution
  •  → Loop runs at least one time

Syntax of do–while Loop

do {
    // statements
} while (condition);

Important:

  • Semicolon ; after while(condition) is mandatory

Simple Example: Print numbers 1 to 5

c
1
2#include <stdio.h>
3
4int main() {
5    int i = 1;
6
7    do {
8        printf("%d\n", i);
9        i++;
10    } while (i <= 5);
11
12    return 0;
13}

Output

1
2
3
4
5

Step-by-Step Working

  1. Loop body executes first
  2. Value is printed
  3. Variable is updated
  4. Condition is checked
  5. If true → repeat
  6. If false → loop stops

Proof that do–while runs at least once

int i = 10;

do {
    printf("Hello");
} while (i < 5);

Output

Hello

Even though the condition is false, the loop runs once.

When to Use do–while Loop

Use do–while when:

  • You must execute code at least once
  • Menu-driven programs
  • Input validation
  • User interaction systems

Example:

  • Show menu once, then ask user whether to continue

while vs do–while

Featurewhiledo–while
Condition checkBefore loopAfter loop
Minimum execution0 times1 time
Loop typeEntry-controlledExit-controlled

C do–while Loop | C Programming | Learn Syntax