Output of the following code is
int i=5;
while(i++<10)
cout<<i;
- AOption (a)
5 6 7 8 9
- BOption (b)
5 6 7 8 9 10
- COption (c)
6 7 8 9
- DOption (d)
6 7 8 9 10
View answer & explanationHide answer & explanation
Correct answer
6 7 8 9 10
Why this answer is correct
The correct answer is : option 4
Key Points
Code:
int i = 5;
while(i++ < 10) cout << i;
Concept:
The expression i++ is a post-increment, meaning:
- The condition i++ < 10 evaluates using the current value of i.
- Then i is incremented after the comparison.
- Inside the loop, cout << i prints the incremented value.
Step-by-Step Execution:
| Loop Iteration | i (before i++) | Condition (i++ < 10) | i (after increment) | Printed |
|---|---|---|---|---|
| 1 | 5 | true | 6 | 6 |
| 2 | 6 | true | 7 | 7 |
| 3 | 7 | true | 8 | 8 |
| 4 | 8 | true | 9 | 9 |
| 5 | 9 | true | 10 | 10 |
| 6 | 10 | false | 11 | — |
Final Output: 6 7 8 9 10
Correct Answer: Option 4) 6 7 8 9 10 ✅