The do/while loop in C is a variation of the while loop, designed to ensure that a block of code is executed at least once, regardless of the condition. Unlike the while loop, which checks the condition before executing the code block, the do/while loop evaluates the condition after the code block executes.
Definition of Do/While Loop
A do/while loop guarantees one execution of the code block, even if the condition is initially false. It is particularly useful for scenarios where the code must run at least once, such as user prompts or menus.
Syntax of Do/While Loop
do {
// Code to execute
} while (condition);
- do: Indicates the beginning of the loop block.
- condition: A logical or relational expression that determines whether the loop will repeat.
- The ; after the while condition is mandatory.
How Do/While Loops Work
- The code block inside the
do
section is executed. - The condition in the
while
statement is then evaluated. - If the condition is true, the loop repeats; otherwise, it terminates.
Example 1: Printing Numbers from 1 to 5
#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 5);
return 0;
}
Output:
1
2
3
4
5
Key Difference Between While and Do/While Loops
Feature | While Loop | Do/While Loop |
---|---|---|
Condition Check | Before executing the loop block | After executing the loop block |
Minimum Execution | May not execute if the condition is false | Executes at least once |
Use Case | When pre-checking the condition is necessary | When guaranteed execution is required |
Example 2: Prompting User Input Until a Valid Number
#include <stdio.h>
int main() {
int number;
do {
printf("Enter a positive number: ");
scanf("%d", &number);
if (number <= 0) {
printf("Invalid input. Try again.\n");
}
} while (number <= 0);
printf("You entered: %d\n", number);
return 0;
}
Output:
Enter a positive number: -5
Invalid input. Try again.
Enter a positive number: 0
Invalid input. Try again.
Enter a positive number: 10
You entered: 10
Use Cases for Do/While Loops
- User Menus: Repeatedly display a menu until the user chooses to exit.
- Input Validation: Ensure a valid input is entered before proceeding.
- Initial Execution Guarantee: Perform an action at least once before checking the condition.
Example 3: Simple Menu Using Do/While
#include <stdio.h>
int main() {
int choice;
do {
printf("\nMenu:\n");
printf("1. Option 1\n");
printf("2. Option 2\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("You selected Option 1.\n");
break;
case 2:
printf("You selected Option 2.\n");
break;
case 3:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Try again.\n");
}
} while (choice != 3);
return 0;
}
Output:
Menu:
1. Option 1
2. Option 2
3. Exit
Enter your choice: 2
You selected Option 2.
Menu:
1. Option 1
2. Option 2
3. Exit
Enter your choice: 3
Exiting program.
Infinite Do/While Loop
A do/while loop becomes infinite when the condition is always true.
Example:
#include <stdio.h>
int main() {
int count = 0;
do {
printf("Infinite Loop Example %d\n", count);
count++;
} while (1); // Always true
return 0;
}
Output:
The message will keep printing indefinitely. To stop the program, use Ctrl+C or terminate it manually.
Nested Do/While Loops
Nested do/while loops allow complex scenarios like printing patterns or processing multi-dimensional arrays.
Example: Multiplication Table
#include <stdio.h>
int main() {
int i = 1, j;
do {
j = 1;
do {
printf("%d x %d = %d\t", i, j, i * j);
j++;
} while (j <= 5);
printf("\n");
i++;
} while (i <= 5);
return 0;
}
Output:
1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 1 x 4 = 4 1 x 5 = 5
2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10
3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15
4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 4 x 5 = 20
5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25
Advantages of Do/While Loops
- Guaranteed Execution: Executes the code block at least once.
- Flexible: Adapts to situations requiring post-condition evaluation.
- Simpler for Certain Scenarios: Reduces the need for duplicate code in input validation.
Common Mistakes to Avoid
- Missing Semicolon: Forgetting the semicolon after the
while
condition results in a syntax error. - Incorrect Condition: Can cause infinite loops or premature termination.
- Misplaced Statements: Ensure proper placement of statements inside the loop block.
Conclusion
The do/while loop in C is a powerful construct for scenarios where the code block must execute at least once before evaluating a condition. Its flexibility makes it ideal for input validation, menu-driven programs, and dynamic condition checking. To explore more examples and advanced C programming concepts, visit Master Coding Science.