Which loop best suits a scenario where the number of iterations is unknown and depends on user input?
Trap 1: for loop
Best for known number of iterations.
Trap 2: for-each loop
Used for iterating over collections/elements.
Trap 3: do-while loop
Executes at least once, may not be appropriate if zero iterations possible.
- A
for loop
Why wrong: Best for known number of iterations.
- B
while loop
Condition checked before each iteration, suitable for unknown iterations.
- C
for-each loop
Why wrong: Used for iterating over collections/elements.
- D
do-while loop
Why wrong: Executes at least once, may not be appropriate if zero iterations possible.