9618 · 11.2
Constructs — FAQ
Frequently asked questions for 9618 Constructs. Direct answers first, then deeper explanation — then practise with marking.
When should I use a `WHILE` loop versus a `REPEAT` loop?
Use a WHILE...DO loop when the loop body might not need to execute at all. The condition is checked at the start, so if it's initially false, the program skips the loop entirely. Use a REPEAT...UNTIL loop when the loop body must execute at least once. The condition is checked at the end. This makes it perfect for tasks like input validation, where you need to get input at least once before you can check if it's valid.
Can I always use `IF` statements instead of a `CASE` statement?
Yes, any CASE statement can be logically rewritten as a series of IF...THEN...ELSEIF statements. However, a CASE statement is often preferred for clarity and readability when you are checking a single variable against a list of discrete, constant values. It makes the code's intent clearer at a glance.
What's the difference in the condition between `WHILE` and `REPEAT` loops?
A WHILE loop continues while its condition is TRUE and stops when it becomes FALSE. A REPEAT loop continues executing and only stops until its condition becomes TRUE. This is a crucial difference to remember when formulating your loop termination conditions.