Home Tutorials C Tutorial Do While Loop
Do While Loop

Do While Loop


The do...while Loop

What is a do...while Loop?

The do...while loop is an exit-controlled loop. Unlike the for and while loops—which check the condition at the very beginning—this loop checks the condition at the end of the loop body.

Key Feature: The body of a do...while loop is guaranteed to execute at least once, even if the condition is false right from the start.

Syntax

do {
    // Statements to execute
    update_expression;
} while (condition); // Note the semicolon at the end!

Practical Examples

Example 1: Guaranteed Execution

int i = 10;

do {
    printf("This will print even though 10 is not less than 5.\n");
    i++;
} while (i < 5);

Example 2: Menu-Driven Program

int choice;

do {
    printf("\n--- Menu ---\n1. Print Hello\n2. Exit\nEnter Choice: ");
    scanf("%d", &choice);

    if (choice == 1) printf("Hello World!\n");

} while (choice != 2);
Common Pitfall: The most common mistake beginners make is forgetting the semicolon (;) after the while condition. In for and standard while loops, there is no semicolon after the condition, but in do...while, it is mandatory!
Example

🏋️ Test Yourself With Exercises

Take our quiz on Do While Loop to test your knowledge.

Browse Quizzes »