Exercises · Q9
Q.Explain the difference between a while loop and a do...while loop. Write a while loop that prints the numbers 1 to 5.
ChseodishaTextbookSubjectiveImportance★★★★★est
69% · 9/13 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →while — entry-controlled. The condition is checked before the body runs. If it is false at the very start, the body is skipped entirely, so a while loop can run zero times:
int i = 1;
while (i <= 5) {
System.out.println(i); // prints 1, 2, 3, 4, 5
i++; // update — moves toward ending the loop
}
do...while — exit-controlled. The body runs first, and the condition is checked afterwards. So the body always runs at least once, even if the condition is false:
int n = 1;
do {
System.out.println(n);
n++;
} while (n <= 5);
The key difference is when the test happens:
while | do...while | |
|---|---|---|
| Condition checked | before body | after body |
| Minimum runs | 0 | 1 |
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.