Running time of a ‘for’ loop is __________ the running time of the statements…
2022
Running time of a ‘for’ loop is __________ the running time of the statements inside the for loop times the number of iterations.
Answer: B. at most — By convention in running-time analysis, a figure such as ‘the running time of the statements inside the for loop’ is read as an upper bound on the work done…
- A.
at least
- B.
at most
- C.
equal to
- D.
independent of
Attempted by 559 students.
Show answer & explanation
Correct answer: B
By convention in running-time analysis, a figure such as ‘the running time of the statements inside the for loop’ is read as an upper bound on the work done in one iteration — including whatever loop-control overhead (the test and increment) is folded into that per-iteration figure — not as a fixed, exact value. Concept: For a loop that executes N iterations, the loop's total running time is the sum of the running times of its body across all iterations. If every iteration's body takes at most c time units to run — the running time of the statements inside the for loop — then the loop's total running time can be at most N × c; this N × c product is only ever an upper bound on the total cost, not a value the loop is guaranteed to reach.
Let c be the running time of the statements inside the for loop (the body), and let N be the number of iterations the loop is set up to perform.
If the loop runs through all N iterations and each one performs the full body work, the total running time equals c added to itself N times, i.e. N × c.
If instead some iterations skip part of the body through a conditional branch, or the loop exits early via a break or return, less than the full N × c amount of work is actually performed.
In every possible execution, the total running time can never exceed N × c — it is bounded above by that product, whether or not the loop happens to do the full work in every pass.
Comparison with the other options:
‘at least’ would require the total time to always reach or exceed N × c, but an early exit or lighter iterations make that guarantee false.
‘equal to’ would require every iteration to do exactly the same fixed amount of work every time, which is only a special case, not a general law.
‘independent of’ is false because the total time is built up directly from N and c — changing either one changes the total.
Hence, the running time of a for loop is at most the running time of the statements inside it times the number of iterations — the relation is an upper bound, matching the option ‘at most’.