Bird
0
0

Why does a for loop sometimes run one less time than expected when using i as the condition?

hard📝 Conceptual Q10 of 15
C - Loops
Why does a for loop sometimes run one less time than expected when using i < n as the condition?
ABecause the initialization is incorrect
BBecause the loop increments i twice each iteration
CBecause the loop stops before i equals n, excluding n
DBecause the loop body is empty
Step-by-Step Solution
Solution:
  1. Step 1: Understand the loop condition i < n

    The loop runs while i is less than n, so it stops once i reaches n.
  2. Step 2: Explain why loop excludes n

    Since i must be less than n, the loop never runs when i equals n, causing one less iteration than if condition was i <= n.
  3. Final Answer:

    Because the loop stops before i equals n, excluding n -> Option C
  4. Quick Check:

    i < n excludes n, so loop runs one less time [OK]
Quick Trick: i < n excludes n, causing one less iteration [OK]
Common Mistakes:
  • Thinking loop increments twice
  • Blaming initialization for iteration count
  • Ignoring loop condition logic

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Quizzes