C Notes
Learn the do-while loop in C with syntax, flowchart, difference from while loop, menu-driven programs, and practical examples. Includes interview questions.
The do-while loop is the exit-controlled loop in C — it guarantees that the loop body executes at least once, regardless of whether the condition is true or false. The condition is checked after the body executes, not before. This makes it fundamentally different from the while loop.
Think of it like this: a while loop is like checking if the pool is warm before jumping in. A do-while loop is jumping in first, and then deciding whether to stay for another lap. The "do first, ask questions later" approach is exactly what makes this loop special.
Syntax of Do-While Loop
do {
// loop body - executes at least once
// statements
} while (condition); // Note the semicolon!Critical Note: The semicolon after while(condition) is mandatory. Forgetting it is a compilation error.
| Component | Description |
|---|---|
do | Keyword starting the loop |
{ } | Loop body (executes first) |
while (condition) | Condition checked after each execution |
; | Mandatory semicolon ending the statement |
Flowchart of Do-While Loop
| (execute) | |
|---|---|
| Condition? |
Basic Example
Count: 1 Count: 2 Count: 3 Count: 4 Count: 5 Loop ended.
The Key Difference: Executes At Least Once
This is the most important feature of do-while. Even if the condition is false from the start, the body runs once:
While loop: (nothing printed) Do-while loop: x = 100
Practical Examples
Example 1: Menu-Driven Program
The do-while loop is perfect for menu-driven programs because you always want to show the menu at least once:
===== Calculator Menu ===== 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Exit Enter your choice (1-5): 1 Enter two numbers: 15 8 Result: 15.00 + 8.00 = 23.00 ===== Calculator Menu ===== 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Exit Enter your choice (1-5): 5 Thank you! Goodbye.
Example 2: Input Validation
Another classic use case — keep asking until valid input is provided:
Enter your age (1-120): -5 Invalid! Age must be between 1 and 120. Enter your age (1-120): 200 Invalid! Age must be between 1 and 120. Enter your age (1-120): 25 Your age is: 25 You are an adult.
Example 3: Sum of Digits
#include <stdio.h>
int main() {
int num, sum = 0;
printf("Enter a positive number: ");
scanf("%d", &num);
int original = num;
do {
sum += num % 10;
num /= 10;
} while (num > 0);
printf("Sum of digits of %d = %d\n", original, sum);
return 0;
}Enter a positive number: 9876 Sum of digits of 9876 = 30
Example 4: Password Verification with Attempts
Enter password: 1111 Wrong password! 2 attempt(s) remaining. Enter password: 2222 Wrong password! 1 attempt(s) remaining. Enter password: 1234 Access granted! Welcome.
While vs Do-While Comparison
| Feature | while | do-while |
|---|---|---|
| Type | Entry-controlled | Exit-controlled |
| Condition check | Before loop body | After loop body |
| Minimum executions | 0 (may never run) | 1 (always runs once) |
| Semicolon | No semicolon after while() | Requires ; after while() |
| Best for | Unknown iterations, may skip | Menu programs, input validation |
| Syntax | while(cond) { } | do { } while(cond); |
When to Use Do-While
Use do-while when:
- You need to execute the body at least once
- Input validation (ask, check, repeat if invalid)
- Menu-driven programs (show menu, process, ask to continue)
- Games (play at least one round)
- Reading data where you process before checking
Use while when:
- The loop might not need to execute at all
- The condition depends on something set before the loop
- File reading (might be empty)
Common Mistakes
Mistake 1: Forgetting the Semicolon
do {
printf("Hello\n");
} while (condition) // ERROR: Missing semicolon!Mistake 2: Wrong Condition Logic
// Want to repeat while input is invalid
do {
scanf("%d", &num);
} while (num > 0); // Wrong! This continues for VALID input
// Correct
do {
scanf("%d", &num);
} while (num <= 0); // Repeats for invalid (non-positive) inputInterview Questions
Q1: What is the minimum number of times a do-while loop executes?
A do-while loop always executes at least once because the condition is evaluated after the loop body runs. Even if the condition is false, the body has already executed once.
Q2: When would you choose do-while over while?
Choose do-while when the loop body must execute at least once — typical examples are menu-driven programs, input validation loops, and "play again?" game loops. If there's a possibility the loop should not run at all, use while instead.
Q3: What happens if you forget the semicolon after do-while?
It causes a compilation error. The semicolon after while(condition) in a do-while is syntactically required — it terminates the do-while statement.
Q4: Can we nest a do-while loop inside a while loop and vice versa?
Yes. Any combination of loop nesting is valid in C. You can nest do-while inside while, for inside do-while, or any other combination. Each loop maintains its own control flow independently.
Q5: Is do { } while(0); useful? When?
Yes! do { } while(0) is a common C idiom used in macros to create a safe multi-statement block that works correctly with if-else. It executes exactly once and the compiler optimizes away the loop overhead entirely.
Summary
The do-while loop is C's exit-controlled loop that guarantees at least one execution of the loop body. It checks the condition after running the body, making it ideal for input validation, menu-driven programs, and any situation where you need to "do something first, then decide whether to repeat." Don't forget the mandatory semicolon after the while condition, and always make sure your condition logic correctly expresses when to continue (not when to stop).
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Do-While Loop in C Programming.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, control, statements, while, loop, do-while loop in c programming
Related C Programming Topics