do-while Loop in C
do-while Loop in C
Introduction
do-while Loop in C is a looping statement used to execute a block of code repeatedly while a condition remains true. Unlike the while loop, the do-while loop executes the loop body at least once before checking the condition.
In this C Programming Course in Jaipur, students learn the syntax, flow, working, and applications of the do-while loop in C programming.
What is do-while Loop in C?
The do-while loop executes the loop body first and checks the condition afterward.
Syntax:
do {
// code
} while(condition);
The loop body always executes at least one time.
Flow of do-while Loop
Execute Loop Body → Check Condition → True → Repeat
Execute Loop Body → Check Condition → False → Exit Loop
The do-while loop is also called:
Exit Controlled Loop
because the condition is checked after execution.
Example of do-while Loop
#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
Understanding the Example
Initialization
int i = 1;
The loop starts from 1.
Loop Body
printf("%d\n", i);
i++;
The value is printed and incremented.
Condition
i <= 5
The loop repeats while the condition is true.
Difference Between while Loop and do-while Loop
| while Loop | do-while Loop |
|---|---|
| Condition checked before execution | Condition checked after execution |
| May execute zero times | Executes at least once |
| Entry controlled loop | Exit controlled loop |
Example Showing Difference
while Loop Example
int i = 10;
while(i <= 5) {
printf("%d", i);
}
Output:
No Output
do-while Loop Example
int i = 10;
do {
printf("%d", i);
} while(i <= 5);
Output:
10
The do-while loop executes once even when the condition is false.
Example Using User Input
#include<stdio.h>
int main() {
int number, sum = 0;
do {
printf("Enter a number: ");
scanf("%d", &number);
sum += number;
} while(number != 0);
printf("Sum = %d", sum);
return 0;
}
The loop continues until the user enters 0.
Nested do-while Loop
A do-while loop inside another do-while loop is called nested do-while loop.
Example:
#include<stdio.h>
int main() {
int i = 1;
do {
int j = 1;
do {
printf("%d %d\n", i, j);
j++;
} while(j <= 2);
i++;
} while(i <= 3);
return 0;
}
Output:
1 1
1 2
2 1
2 2
3 1
3 2
Applications of do-while Loop
The do-while loop is commonly used in:
- Menu-driven programs
- ATM systems
- Game menus
- Input validation systems
- Login systems
Students learning C Programming Course in Jaipur use do-while loops in many interactive applications.
Infinite do-while Loop
Example:
do {
printf("Infinite Loop");
} while(1);
This creates an infinite loop because the condition always remains true.
Common Errors in do-while Loop
Missing Semicolon
Incorrect:
} while(i <= 5)
Correct:
} while(i <= 5);
The semicolon after while(condition) is mandatory.
Missing Increment
Incorrect:
do {
printf("%d", i);
} while(i <= 5);
This creates an infinite loop.
Correct:
i++;
Best Practices
- Initialize loop variables properly
- Update loop counters correctly
- Avoid unnecessary infinite loops
- Use meaningful conditions
- Keep loops readable and simple
Good loop structure improves program maintainability.
Importance of do-while Loop in C
The do-while loop helps:
- Execute code at least once
- Handle user-driven applications
- Build menu-based systems
- Automate repeated tasks
Understanding do-while loops is essential for practical programming.
Real-World Example
ATM Menu:
do {
printf("1. Deposit\n");
printf("2. Withdraw\n");
printf("3. Exit\n");
scanf("%d", &choice);
} while(choice != 3);
The menu continues until the user selects Exit.
Summary
do-while Loop in C is an exit-controlled loop that executes code first and checks the condition afterward. It guarantees at least one execution of the loop body.
This lesson explained syntax, flow of execution, examples, nested loops, applications, infinite loops, and best practices for do-while loops in C programming.
FAQs
What is do-while Loop in C?
The do-while loop executes code repeatedly while a condition remains true.
Why is do-while called exit controlled loop?
Because the condition is checked after loop execution.
What is the main difference between while and do-while?
The do-while loop executes at least once even if the condition is false.
Is semicolon mandatory in do-while loop?
Yes, semicolon after while(condition) is mandatory.
Where is do-while loop commonly used?
It is commonly used in menu-driven and interactive applications.
