ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains

The loop below prints the running count for i = 1, 4, 7, 10, ... up to and including 20 (i increases by 3 each time). What is the number of loop iterations printed?

The loop below prints the running count for i = 1, 4, 7, 10, ... up to and including 20 (i increases by 3 each time). What is the number of loop iterations printed?
count = 0
for i = 1 to 20 step 3
    count = count + 1
end for
Print count
  1. A. 5
  2. B. 10
  3. C. 20
  4. D. 7

Correct answer: D. 7

The values 1, 4, 7, 10, 13, 16, 19 are 7 iterations. Stepping by 4 gives 5 iterations, a step of 2 gives 10, and counting to 20 itself gives 20.

More Pseudocode and Code Tracing questions

Exams that ask this topic