An IT technician is writing a program that needs to perform a task exactly 10 times. Which code structure should the technician use to repeat the task the specified number of times?
if statement
for loop
while loop without a counter or condition that stops at 10
do-while loop that checks a condition unrelated to the iteration count
A for loop is ideal when the exact number of iterations is known before the loop starts, such as 10 times. The loop header initializes a counter, tests it against the limit, and automatically updates it, ensuring the block runs precisely the required number of times. An if statement evaluates a condition only once and does not repeat. A while loop without a counter or a condition tied to 10 could loop indefinitely or too few times. A do-while loop also relies on its terminating condition; if that condition is unrelated to an iteration counter, the loop might not execute exactly 10 times.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
Why is a for loop preferred for known iteration counts?
Open an interactive chat with Bash
What is the difference between a while loop and a for loop?
Open an interactive chat with Bash
Can a do-while loop be used for a fixed number of iterations?