- What is a while loop and how it works
- What is a do-while loop and how it works
- The key differences between while and do-while loops
- When to use each loop type
- Complete program examples with explanations
Introduction
In this tutorial section, we will learn the difference between the while loop and the do-while loop in the C programming language.
Both while and do-while loops are types of iterative (looping) statements used in C. However, there are some key differences between them. In this tutorial, we will compare them in a tabular form. But first, let's briefly understand what each loop does.
What is a while Loop?
A while loop in C is a control flow statement that repeatedly executes a block of code as long as a given Boolean condition is true. In other words, a while loop is a type of iterative or repeating statement.
💡 Key Point: The while loop is an entry-controlled loop. It checks the condition before executing the loop body.
What is a do-while Loop?
A do-while loop is known as an Exit-Controlled Loop. It is similar to a while loop, except that it guarantees the loop body will execute at least once.
The main difference is that a do-while loop checks the condition after executing the loop body, whereas a while loop checks the condition before execution.
💡 Key Point: The do-while loop is an exit-controlled loop. It checks the condition after executing the loop body, so it always runs at least once.
Key Differences Between while and do-while Loop
| Feature | while Loop | do-while Loop |
|---|---|---|
| Condition Check | Checked before execution | Checked after execution |
| Minimum Executions | 0 (may not run at all) | 1 (always runs at least once) |
| Loop Type | Entry-Controlled | Exit-Controlled |
| Syntax | while(condition) |
do { } while(condition); |
| Semicolon | No semicolon after condition | Semicolon required after while(condition); |
| Curly Braces | Optional for single statement | Required even for single statement |
| Initialization | Must be done before the loop | Can be done before or inside the loop |
🎯 Quick Summary: while loop = Check condition first, then execute. do-while loop = Execute first, then check condition.
Flowchart Comparison
The diagram below shows the difference between while and do-while loops in C programming.
Let's break it down and understand how these two loops differ — one step at a time.
1. Syntax:
while loop:
while(condition) {
// code inside the loop
}
do-while loop:
do {
// code inside the loop
} while(condition);
2. When the condition is checked:
- while loop: The condition is checked before the loop starts. So if the condition is false right away, the loop won't run at all.
- do-while loop: Here, the condition is checked after the loop runs. That means the code inside the loop will run at least once, no matter what.
3. How iteration works:
- while loop: If the condition is false at the beginning, the loop body won't run.
- do-while loop: The loop body runs once before checking the condition — so it always runs at least once.
4. Flow of control:
In a while loop, control goes straight to the condition check. In a do-while loop, it first goes to the loop body, then checks the condition. That's the key difference in how they work behind the scenes.
C Program to Illustrate while Loop
The program below displays numbers from 1 to 10 using a while loop.
#include <stdio.h>
int main() {
int i = 1;
while(i <= 10) {
printf("\n%d", i);
i++;
}
return 0;
}
Sample Output:
1 2 3 4 5 6 7 8 9 10
Explanation:
- We declare an integer variable
iand initialize it to 1 usingint i = 1;. - The program checks the condition in the
whileloop:i <= 10. Since 1 is less than or equal to 10, the condition is true and the loop begins executing. - Inside the loop,
printf("\n%d", i);prints the current value ofi, which is 1. - Then,
i++;increases the value ofiby 1, soibecomes 2. - The control goes back to the top of the
whileloop and checks the condition again:2 <= 10. Since it's still true, the loop continues. - This process repeats until
ibecomes 11. At that point, the conditioni <= 10is false, and the loop stops.
C Program to Illustrate do-while Loop
The program below displays numbers from 1 to 10 using a do-while loop.
#include <stdio.h>
int main() {
int i = 1;
do {
printf("\n%d", i);
i += 1;
} while(i <= 10);
return 0;
}
Sample Output:
1 2 3 4 5 6 7 8 9 10
Explanation:
- The variable
iis declared as an integer and initialized to 1 usingint i = 1;. - Since this is a do-while loop, the program begins by executing the loop body first — without checking the condition.
- The
printf("\n%d", i);statement prints 1 to the screen. - Then
i += 1;increments the value ofiby 1, so nowibecomes 2. - After that, the program checks the condition in the
whilepart:i <= 10. Since 2 is less than or equal to 10, the condition is true, and the loop runs again. - This process continues — printing each value of
ifrom 1 to 10 — untilibecomes 11. At that point, the conditioni <= 10becomes false, and the loop terminates. - Note: The
do-whileloop always executes its body at least once, even if the condition is false at the start.
💻 Practice Exercise
Challenge: Write a program using a do-while loop that asks the user to enter a number. The loop should keep asking until the user enters a number greater than 100. Compare this with a while loop implementation.
🔍 Click to Show Solution
#include <stdio.h>
int main() {
int num;
// Using do-while loop (guarantees at least one execution)
do {
printf("Enter a number greater than 100: ");
scanf("%d", &num);
} while(num <= 100);
printf("You entered: %d\n", num);
return 0;
}
📝 Summary
- while loop: Entry-controlled, checks condition before execution, may run 0 times
- do-while loop: Exit-controlled, checks condition after execution, always runs at least once
- while syntax:
while(condition) { } - do-while syntax:
do { } while(condition);(note the semicolon) - Choose while when you want to check condition before execution
- Choose do-while when you want the code to execute at least once
Frequently Asked Questions
1. What is the main difference between while and do-while loop?
The main difference is that a while loop checks the condition first (entry-controlled) and may not execute at all, while a do-while loop checks the condition after execution (exit-controlled) and always executes at least once.
2. When should I use a while loop vs do-while loop?
Use a while loop when you want to check the condition before executing the code (like file reading, user validation). Use a do-while loop when you want the code to run at least once (like menu-driven programs, user input validation where you want to show the prompt first).
3. Is semicolon required in do-while loop?
Yes! The semicolon ; after while(condition) is required in a do-while loop. This is one of the most common mistakes beginners make.
4. Can both while and do-while create infinite loops?
Yes, both can create infinite loops if the condition never becomes false. For example, while(1) or do { } while(1); will run forever.
5. Which loop is better for menu-driven programs?
The do-while loop is typically better for menu-driven programs because you want to display the menu at least once before asking the user to exit.
💡 Tip: Remember: while = "Check first, then do" | do-while = "Do first, then check"