BCA Sem 1 | Unit 2 | Computer Concepts & C Programming
Do...While Loop in C Language — BKNMU Junagadh
Syntax, Exit-Controlled Looping Mechanism, Menu-Driven Applications, and Comparison with While Loop
do...while loop is an exit-controlled iteration statement in C programming. Unlike for and while loops, it evaluates its test condition at the end of the loop body, guaranteeing that the loop statements execute at least once regardless of whether the initial condition is True or False.
The do...while loop is built around four fundamental concepts that distinguish it from other C loops:
1. Body First Execution
The loop body executes immediately upon entry before checking any condition, ensuring a minimum of one execution cycle.
2. Bottom Condition Check
The condition is tested at the end of every cycle. If True (non-zero), execution loops back to the top of the do block.
3. Mandatory Semicolon
Unlike other control structures, a semicolon (;) is strictly required right after the closing condition parenthesis.
4. Interactive Applications
Ideal for menu-driven programs where options must be shown to the user at least once before asking to exit or continue.
Here is a detailed comparative summary between entry-controlled while loops and exit-controlled do...while loops in C:
| Comparison Aspect | While Loop (while) | Do...While Loop (do...while) |
|---|---|---|
Control Type | Entry-Controlled: Evaluates condition before executing loop body. | Exit-Controlled: Evaluates condition after executing loop body. |
Minimum Executions | 0 times (if the initial condition is False, body is skipped entirely). | 1 time (guaranteed to execute at least once even if initial condition is False). |
Semicolon Usage | No semicolon after loop header (e.g., while(cond)). | Requires a semicolon at the end (e.g., while(cond);). |
Primary Use Case | When iteration count depends entirely on a pre-checked condition. | Interactive user menus, input validation, and user prompt prompts. |
do...while loop whenever you need to process input or display options before checking if the action should be repeated—such as repeatedly asking a user for password entry until it matches!
1. Basic Do...While Loop (Printing Numbers 1 to 5):
#include <stdio.h>
int main() {
int i = 1;
// Guaranteed first run before condition test
do {
printf("Value of i: %d\n", i);
i++;
} while (i <= 5);
return 0;
}
2. Execution Proof (Condition Starts False):
#include <stdio.h>
int main() {
int num = 100;
// Executes once even though num < 10 is False!
do {
printf("This prints at least ONCE! num = %d\n", num);
} while (num < 10);
return 0;
}
3. Practical Menu-Driven Calculator Example:
#include <stdio.h>
int main() {
int choice;
do {
printf("\n--- BCA SCHOOL MENU ---\n");
printf("1. Display Hello\n");
printf("2. Display Unit 2 Notes\n");
printf("3. Exit Program\n");
printf("Enter your choice (1-3): ");
scanf("%d", &choice);
if (choice == 1) {
printf("Hello, Student!\n");
} else if (choice == 2) {
printf("Unit 2 covers Decision Control & Loops.\n");
}
} while (choice != 3);
printf("Exited successfully.\n");
return 0;
}
Step 1: Execute Loop Body
Control flows directly into the do block and executes all internal statements sequentially.
Step 2: Update Counter / Process Inputs
Variables inside the block are updated or user input is accepted.
Step 3: Evaluate Bottom Condition
The expression in while(condition); is tested. If True (non-zero), execution jumps back to Step 1. If False (zero), the loop terminates.
do { ... } while (condition); — note the semicolon after the parenthesis.
do body, the loop condition will stay True permanently, leading to an infinite execution loop!
- Loop Type: Exit-Controlled Iteration Statement.
- Minimum Executions: Exactly 1 execution cycle guaranteed.
- Key Syntax Rule: Ends with a mandatory trailing semicolon (
while(condition);). - Condition Position: Bottom of the loop structure.
- Best Practical Use: Interactive menu systems and user input validation loops.