Published by Patrick Mutisya · 14 days ago
Use pseudocode to write a post‑condition loop (repeat‑until loop).
A post‑condition loop executes its body first and then evaluates the termination condition. Consequently the body is guaranteed to run at least once.
REPEAT
/* statements */
UNTIL condition
REPEAT … UNTIL.| Aspect | Pre‑condition Loop (WHILE) | Post‑condition Loop (REPEAT … UNTIL) |
|---|---|---|
| When is the condition evaluated? | Before the first execution of the body. | After each execution of the body. |
| Guarantee of at least one execution | No – body may never run. | Yes – body runs at least once. |
| Typical syntax | WHILE condition DO … END WHILE | REPEAT … UNTIL condition |
| Example condition | \$i \le 10\$ | \$i > 10\$ |
REPEAT block.UNTIL keyword.Goal: Compute \$S = 1 + 2 + \dots + n\$ where \$n\$ is entered by the user.
Pseudocode using a post‑condition loop:
READ n
sum ← 0
i ← 1
REPEAT
sum ← sum + i
i ← i + 1
UNTIL i > n
OUTPUT sum
i > n is checked after each addition, ensuring the loop stops exactly after the \$n^{\text{th}}\$ term has been added.