AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Mediumdo whileloop semantics

~75s

A post-tested loop

A Repeat ... Until loop tests its condition after running the body. What is printed?

Integer x, c
Set x = 3
Set c = 0
Repeat
    Set x = x - 7
    Set c = c + 1
Until x < 5
Print c
Show the answer and the worked solution

Correct answer: B.

1

Worked solution

  1. x starts at 3, so the exit condition x < 5 is already true — but the test comes after the body, not before.
  2. The body therefore runs once: x = 3 - 7 = -4 and c = 1.
  3. The test now finds -4 < 5, so the loop ends and 1 is printed.

Answer: 1.

The idea

A post-tested loop executes its body at least once whatever the condition says. A pre-tested While with the same condition would have printed 0, and that contrast is the whole point of the question.

Shortcut. Rule out 0 first, since a post-tested loop cannot print it; one subtraction then takes x to −4, already below 5, so the count stops at 1.

Common trap. 0 treats the loop as pre-tested: x = 3 already satisfies x < 5, but the condition is checked only after the body has run.

A post-tested loop — Pseudocode Aptitude Question

A post-tested loop: A Repeat ... Until loop tests its condition after running the body. What is printed? Answer and worked solution on CodeKairo.

Question

A Repeat ... Until loop tests its condition after running the body. What is printed?

Integer x, c
Set x = 3
Set c = 0
Repeat
    Set x = x - 7
    Set c = c + 1
Until x < 5
Print c

Options

  1. 0
  2. 1
  3. 2
  4. 3
Show the answer and the worked solution

Correct answer: B. 1

Worked solution

  1. x starts at 3, so the exit condition x < 5 is already true — but the test comes after the body, not before.
  2. The body therefore runs once: x = 3 - 7 = -4 and c = 1.
  3. The test now finds -4 < 5, so the loop ends and 1 is printed.

Answer: 1.

The idea

A post-tested loop executes its body at least once whatever the condition says. A pre-tested While with the same condition would have printed 0, and that contrast is the whole point of the question.

Shortcut. Rule out 0 first, since a post-tested loop cannot print it; one subtraction then takes x to −4, already below 5, so the count stops at 1.

Common trap. 0 treats the loop as pre-tested: x = 3 already satisfies x < 5, but the condition is checked only after the body has run.

All Pseudocode questions · placement mock tests