Do While Loop in C Programming
Do While Loop in C Programming
Introduction to Do While Loop in C Programming
The do while loop in C programming is used to execute a block of code at least once, even if the condition is false. It is different from the while loop because the condition is checked after execution.
The do while loop is useful when you want the loop to run at least one time.
Syntax of Do While Loop in C Programming
// code block
} while(condition);
How Do While Loop Works
- The loop body executes first
- Then the condition is checked
- If the condition is true, the loop repeats
- If the condition is false, the loop stops
Basic Example of Do While Loop
int main() {
int i = 1;
do {
printf(“%d\n”, i);
i++;
} while(i <= 5);
return 0;
}
Example: Execute At Least Once
int main() {
int i = 10;
do {
printf(“This runs once”);
} while(i < 5);
return 0;
}
Even though the condition is false, the loop runs once.
Do While Loop with User Input
int main() {
int number;
do {
printf(“Enter a number (0 to stop): “);
scanf(“%d”, &number);
printf(“You entered: %d\n”, number);
} while(number != 0);
return 0;
}
Difference Between While and Do While Loop
While Loop
- Condition checked first
- May not execute at all
Do While Loop
- Executes at least once
- Condition checked after execution
Common Use Cases of Do While Loop
Where Do While Loop is Used
- Menu-driven programs
- Input validation
- Programs that must run at least once
- User interaction systems
Advantages of Do While Loop
Key Benefits
- Ensures at least one execution
- Useful for user input
- Simple and easy to use
- Good for validation logic
Start Learning C Programming
Practice do while loop programs to understand loop behavior in C programming.
Summary
The do while loop in C programming executes the code at least once and then checks the condition. It is useful when at least one execution is required.
FAQs
What is do while loop in C programming?
It is a loop that executes code first and then checks the condition.
What is the difference between while and do while?
While checks condition first, do while executes first.
When should we use do while loop?
When the code must run at least once.
Can do while loop run infinite times?
Yes, if the condition never becomes false.



