Do While Loop
Introduction to C
Structure of a C Program
Variables and Data Types
Input and Output
Operators in C
Decision Making
If Statement
if...else Statement
Nested if Statement
Nested if...else Statement
Switch Statement
Loops
For Loop
While Loop
Do While Loop
Loop Comparison
Arrays
Strings
Functions
Function Types
Library Functions
User-Defined Functions
Recursion Function
Pointers
Structures
File Handling
Common Mistakes
Ideas
Do While Loop
The do...while Loop
What is a do...while Loop?
The do...while loop is an exit-controlled loop. Unlike the for and while loops—which check the condition at the very beginning—this loop checks the condition at the end of the loop body.
Key Feature: The body of a do...while loop is guaranteed to execute at least once, even if the condition is false right from the start.
Syntax
do {
// Statements to execute
update_expression;
} while (condition); // Note the semicolon at the end!
// Statements to execute
update_expression;
} while (condition); // Note the semicolon at the end!
Practical Examples
Example 1: Guaranteed Execution
int i = 10; do { printf("This will print even though 10 is not less than 5.\n"); i++; } while (i < 5);
Example 2: Menu-Driven Program
int choice; do { printf("\n--- Menu ---\n1. Print Hello\n2. Exit\nEnter Choice: "); scanf("%d", &choice); if (choice == 1) printf("Hello World!\n"); } while (choice != 2);
Common Pitfall: The most common mistake beginners make is forgetting the semicolon (;) after the
while condition. In for and standard while loops, there is no semicolon after the condition, but in do...while, it is mandatory!
Example
Executing...
❌ Error:
✅ Output:
// Click Run ▶ to execute
🏋️ Test Yourself With Exercises
Take our quiz on Do While Loop to test your knowledge.
Browse Quizzes »