Difference between while and do–while in Java - Performance Comparison
We want to see how the time it takes to run loops changes with input size.
Specifically, we compare two loop types: while and do-while.
Analyze the time complexity of these two loops.
int i = 0;
while (i < n) {
System.out.println(i);
i++;
}
int j = 0;
do {
System.out.println(j);
j++;
} while (j < n);
Both loops print numbers from 0 up to n-1, but the order of checking the condition differs.
Both loops repeat printing and incrementing.
- Primary operation: Printing and incrementing inside the loop.
- How many times: Exactly n times, assuming n > 0.
As n grows, the number of print operations grows roughly the same for both loops.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 prints |
| 100 | About 100 prints |
| 1000 | About 1000 prints |
Pattern observation: The work grows linearly with n for both loops.
Time Complexity: O(n)
This means the time to run grows directly in proportion to the input size.
[X] Wrong: "do-while loops always run slower because they run the loop body before checking the condition."
[OK] Correct: Both loops run the body about n times, so their time grows the same way; the difference is only when the condition is checked.
Understanding how loops grow with input helps you write efficient code and explain your choices clearly.
What if the loop body had a nested loop running n times inside? How would the time complexity change?
Practice
while and do-while loops in Java?Solution
Step 1: Understand
Thewhileloop behaviorwhileloop checks the condition before running the loop body, so if the condition is false initially, the loop body does not run at all.Step 2: Understand
Thedo-whileloop behaviordo-whileloop runs the loop body once first, then checks the condition to decide if it should continue looping.Final Answer:
whilechecks the condition before running;do-whileruns once before checking. -> Option CQuick Check:
Condition check timing =whilebefore,do-whileafter [OK]
- Thinking both loops check condition before running
- Assuming do-while never checks condition
- Confusing which loop runs code first
do-while loop in Java?Solution
Step 1: Recall
Thedo-whilesyntaxdo-whileloop syntax starts withdo, then the loop body in braces, followed bywhilewith the condition in parentheses and a semicolon.Step 2: Check each option
do { /* code */ } while (condition); matches the correct syntax exactly. Options B, C, and D have syntax errors or wrong order.Final Answer:
do { /* code */ } while (condition); -> Option BQuick Check:
Syntax order: do, body, while(condition); [OK]
- Missing semicolon after while(condition)
- Putting condition after do without while
- Using while without do in do-while loop
int count = 0;
do {
System.out.print(count + " ");
count++;
} while (count < 3);Solution
Step 1: Trace the loop execution
The variablecountstarts at 0. Thedo-whileloop printscountthen increments it. It repeats whilecount < 3.Step 2: List printed values
First iteration: prints 0, count becomes 1; second: prints 1, count 2; third: prints 2, count 3; loop stops because 3 is not less than 3.Final Answer:
0 1 2 -> Option DQuick Check:
Prints 0 to 2 inclusive [OK]
- Assuming count starts at 1
- Expecting output to include 3
- Thinking loop doesn't run if condition false initially
int i = 5;
while (i > 0);
{
System.out.println(i);
i--;
}Solution
Step 1: Analyze the while loop syntax
The semicolon immediately afterwhile (i > 0);ends the loop statement, making it an empty loop that runs infinitely if condition is true.Step 2: Understand the block after while
The block in braces is not part of the loop but runs once after the loop ends, which never happens here due to infinite loop.Final Answer:
Extra semicolon after while condition causes infinite loop -> Option AQuick Check:
Semicolon after while() ends loop prematurely [OK]
- Thinking braces fix the semicolon error
- Assuming loop runs once
- Confusing condition correctness with syntax error
Solution
Step 1: Understand the requirement
The program must ask the user at least once to enter a positive number, then repeat if the input is invalid.Step 2: Choose the suitable loop
Ado-whileloop runs the prompt first, then checks the condition to repeat, ensuring the prompt always appears at least once.Final Answer:
Use a do-while loop because it ensures the prompt runs at least once. -> Option AQuick Check:
Prompt runs once before condition check = do-while [OK]
- Choosing while loop and missing first prompt
- Using for loop which is not for input validation
- Using if statement which doesn't loop
Ready to prove it?
Pass the test and this pattern moves unknown
