Table of Contents
What is a Do-While Loop in C?
A do-while loop is a post-tested loop, meaning the condition is evaluated after the execution of the loop body. This guarantees that the loop body executes at least once.Syntax:
Explanation:do {
// code to be executed
}
- The do block contains the code to execute.
- The while condition is checked after execution.
- If the condition is true, the loop runs again.
- If false, the loop stops.
Example 1: Basic Do-While Loop
Below is the C program for the basic do-while loop:
// C program to show basic do-while loop
#include stdio.h
int main()
{
int i = 1;
do
{
printf("%d\n", i);
i++;
} while (i = 5);
return 0;
}
Output:
Explanation:1
2
3
4
5
The loop starts with i = 1. It prints the value and increments it. After executing the block, the condition i = 5 is checked. The loop continues until the condition becomes false.
Example 2: Loop Executes At Least Once
Below is the C program to show a loop that executes at least once:
// C program to show loop that executes
// at least once
#include stdio.h
int main()
{
int i = 10;
do {
printf("Value: %d\n", i);
i++;
} while (i 5);
return 0;
}
Output:
Explanation:Value: 10
Even though the condition i 5 is false from the start, the loop executes once because the condition is checked after execution.
Example 3: User Input Using Do-While Loop
Below is the C program to show user input using do-while loop:
// C program to show user input using
// do-while loop
#include stdio.h
int main()
{
int num;
do {
printf("Enter a number (0 to exit): ");
scanf("%d", &num);
} while (num != 0);
printf("Loop ended.\n");
return 0;
}
Output:
Explanation:Enter a number (0 to exit): 3
Enter a number (0 to exit): 4
Enter a number (0 to exit): 0
Loop ended.
The loop continues to take input until the user enters 0. This is a common real-world use case where at least one input is required.
Key Features of Do-While Loop
- Executes the Code at Least Once: The do-while loop always executes the loop body one time before checking the condition. This makes it useful when at least one execution is necessary.
- Condition is Checked After Execution: In a do-while loop, the condition is evaluated after the statements inside the loop are executed. Because of this, it is known as a post-tested loop.
- Useful for Menu-Driven Programs: The do-while loop is commonly used in programs that display menus repeatedly until the user chooses to exit.
- Reduces Repetitive Code: It helps avoid writing the same code multiple times by executing a block repeatedly based on a condition.
- Easy to Use with User Input Validation: The do-while loop is ideal for taking user input repeatedly until valid data is entered because the loop executes before checking the condition.
While vs Do-While Loop
Below is the difference between while and do-while loop:| Feature | While Loop | Do-While Loop |
|---|---|---|
| Condition Check | Before execution | After execution |
| Minimum Execution | May not run at all | Run at least once |
| Use Case | When condition must be checked first | When execution is required |
Common Mistakes in Do-While Loop
- Forgetting the Semicolon After While Condition: Many beginners forget to place a semicolon after the while(condition) statement, which causes a syntax error.
- Creating an Infinite Loop: If the loop variable is not updated properly or the condition never becomes false, the loop can run forever.
- Using the Wrong Condition: Writing an incorrect condition may cause the loop to stop too early or continue longer than expected.
- Not Initializing Variables Properly: If variables used in the loop condition are not initialized before the loop starts, the program may produce unpredictable results.
- Misunderstanding Loop Execution: Some programmers expect the condition to be checked before execution, but in a do-while loop the code runs first and the condition is checked afterward.
Conclusion
The do-while loop in C is simple but powerful when used correctly. It guarantees execution at least once, making it ideal for user-driven programs. Understanding when to use it over other loops can improve both logic and efficiency. With proper handling of conditions, it becomes a reliable tool in any C programmer’s toolkit.Frequently Asked Questions
1. What is the main difference between while and do-while loop?2. Does a do-while loop always execute at least once?The while loop checks the condition before execution, whereas the do-while loop checks it after execution.
3. Can a do-while loop become infinite?Yes, the loop body runs at least one time regardless of the condition.
4. Where is do-while loop commonly used?Yes, if the condition never becomes false, it will run indefinitely.
5. Is the semicolon necessary in do-while loop syntax?It is commonly used in menu-driven programs and input validation scenarios.
Yes, the semicolon after the while condition is mandatory.
0 Comments