C Do/While Loop
Introduction to Do/While Loop
The do/while loop in C is a variation of the while loop that tests the condition at the end of the loop body. This means the loop body always executes at least once, regardless of whether the condition is initially true or false.
It's called a 'post-test loop' because the condition is evaluated after the execution of the loop body.
Syntax and Structure
The syntax of a do/while loop is (note the semicolon at the end):
do {
// code to be executed
} while (condition);
How Do/While Loop Works
1. The code inside the do block is executed first
2. Then the condition is evaluated
3. If the condition is true (non-zero), the loop repeats
4. If the condition is false (zero), the loop terminates
5. The program continues with the next statement after the loop
Basic Example
#include <stdio.h>
int main(void) {
int count = 1;
// Print numbers from 1 to 5
do {
printf("%d ", count);
count++; // Increment counter
} while (count <= 5);
printf("\nLoop ended.\n");
return 0;
}
1 2 3 4 5 Loop ended.
Key Difference from While Loop
The crucial difference between while and do/while loops is when the condition is evaluated:
• while loop: Condition is evaluated before the loop body (may execute 0 times)
• do/while loop: Condition is evaluated after the loop body (always executes at least once)
#include <stdio.h>
int main(void) {
int x = 10;
// This while loop won't execute at all
while (x < 5) {
printf("While loop: %d\n", x);
x++;
}
// This do/while loop will execute once
do {
printf("Do/While loop: %d\n", x);
x++;
} while (x < 5);
return 0;
}
Do/While loop: 10
Practical Use Cases
Do/while loops are particularly useful when:
1. You need to execute code at least once (e.g., menu systems)
2. Processing user input where you need to ask at least once
3. When the loop condition depends on calculations within the loop body
#include <stdio.h>
int main(void) {
int number;
int rc;
// Ask for input at least once
do {
printf("Enter a positive number: ");
rc = scanf("%d", &number);
if (rc != 1) {
// Clear invalid input
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
printf("Invalid input. Please try again.\n");
number = 0; // force repeat
continue;
}
if (number <= 0) {
printf("Invalid input. Please try again.\n");
}
} while (number <= 0);
printf("You entered: %d\n", number);
return 0;
}
Enter a positive number: -5 Invalid input. Please try again. Enter a positive number: 0 Invalid input. Please try again. Enter a positive number: 7 You entered: 7
Loop Control in Do/While
Like while loops, do/while loops support break and continue statements:
• break: Immediately exits the entire loop
• continue: Skips the remaining code in the current iteration and then evaluates the condition
#include <stdio.h>
int main(void) {
int i = 0;
do {
i++;
if (i == 3) {
continue; // Skip the rest of this iteration
}
if (i == 8) {
break; // Exit the loop entirely
}
printf("%d ", i);
} while (i < 10);
printf("\nLoop ended at i = %d\n", i);
return 0;
}
1 2 4 5 6 7 Loop ended at i = 8
Common Pitfalls and Best Practices
1. Don't forget the semicolon after the while condition
2. Ensure the loop condition will eventually become false to avoid infinite loops
3. Use do/while when you need to execute the loop body at least once
4. Consider using while loops when the body might not need to execute at all
5. Be careful with variables that might be modified in the loop condition
6. Remember: with do/while, a continue jumps to the condition check (not to the top of the block)