ProgrammingHardAccenture · Cognizant pattern

What will be the output of the following pseudocode (a do-while-style loop that always runs at least once)?

What will be the output of the following pseudocode (a do-while-style loop that always runs at least once)?
Integer x = 10, c = 0
repeat
    x = x - 3
    c = c + 1
until (x <= 0)
Print x, c
  1. A. 1 3
  2. B. -2 4
  3. C. 0 4
  4. D. -2 3

Correct answer: B. -2 4

x goes 10->7->4->1->-2 over 4 iterations; the until condition (x<=0) is only checked AFTER each iteration, so it keeps going through x=1 (still >0) and stops only once x becomes -2.

More Pseudocode and Code Tracing questions

Exams that ask this topic