Skip to content

Checking Before You Start

A loop that asks its question first, so the work can happen no times at all.

  • Foundations

REPEAT asks its question at the bottom, after the work. That suits a guessing game: you have to make one guess before anyone can say whether you were right.

Sometimes the work should not happen at all. Then the question goes first.

In the lives examples, start with a non-negative whole number. Each completed pass subtracts exactly one; no other instruction changes lives. We use ordinary integer arithmetic here, not a target machine’s wrapping byte counter.

Ask, then work

WHILE checks before each pass, including the first one.

Code
LET lives = 3
WHILE lives > 0 DO
  SHOW "Lives left: ", lives
  LET lives = lives - 1
END
SHOW "Game over"
Output
Lives left: 3
Lives left: 2
Lives left: 1
Game over

The computer asks lives > 0, and runs the block only if the answer is yes. At the bottom it goes back and asks again. When lives reaches 0 the answer is no, and the program carries on below the END.

ask againlives > 0 ?show andtake one awaySHOW "Game over"
The question comes first, so the arrow back arrives above the work rather than below it. That is the whole difference between this loop and the last one.

What makes this loop finish?

Check before work Work runs? Value after subtraction
lives = 3 yes 2
lives = 2 yes 1
lives = 1 yes 0
lives = 0 no 0

Each completed pass makes the positive whole count smaller. It cannot keep decreasing by one while remaining positive forever. Starting at 3 therefore gives three passes and then the false test at 0, provided each pass completes. This explains progress for these stated rules; changing some value is not enough to establish that every loop terminates.

A reset in the wrong place

Suppose we move the starting assignment inside the body:

Code
LET lives = 3
WHILE lives > 0 DO
  LET lives = 3
  SHOW "Lives left: ", lives
  LET lives = lives - 1
END

The first pass leaves lives at 2. On every following pass the reset puts it back to 3, and subtraction leaves it at 2 again. The next test stays true. Under these rules, with no outside change, the same state repeats instead of approaching zero. The subtraction is present, but the reset undoes progress.

Does moving LET lives = 3 before WHILE repair this loop? Where should a new game restore its starting lives?

Show the explanation

Yes. Initialise once before this loop, then let each pass decrease lives. A new game can restore the starting value at its own boundary. Resetting it on every pass changes the rule and prevents this loop from counting down to zero.

None is a real number of times

Start with no lives at all and the block never runs:

Code
LET lives = 0
WHILE lives > 0 DO
  SHOW "Lives left: ", lives
  LET lives = lives - 1
END
SHOW "Game over"
Output
Game over

That is the difference worth holding on to. A REPEAT always runs its work at least once, because it does not ask until afterwards. A WHILE can run it no times.

Which you want depends on the work. Asking for a guess suits REPEAT: there is no answer to check until somebody has guessed. Spending lives suits WHILE: a player with none should not get a turn.

With lives starting at 0, how many times does WHILE run? What if the same body uses REPEAT ... UNTIL lives <= 0?

Show the explanation

WHILE runs no passes. The post-test version starts the body once: it shows Lives left: 0 and subtracts one to give -1 before checking the condition. That is why checking first is the right choice for this no-lives, no-turn rule. The negative result is ordinary arithmetic in this model, not a byte-underflow claim.

A saved flag does not update because lives changed. If the loop tests a saved answer, state when it is refreshed; that is a different rule from testing lives > 0 again each time.

When it’s wrong, see why

  • It never stops. Trace the value used by the condition. In this lives example, omitting subtraction or resetting inside the body defeats progress. Other loops may depend on outside input; state what is expected to change.
  • It never runs. The question was already no on the first ask. That is not a fault in itself, but check it is what you wanted.
  • You wanted it to run once whatever happens. That is a REPEAT. WHILE promises nothing about the first pass.

What you’ve learnt

  • WHILE asks its question first and runs the work only while the answer is yes.
  • A WHILE can run no times at all. A REPEAT always runs at least once.
  • Pick by the work: check first when doing it wrongly would be a mistake, check afterwards when there is nothing to check until you have.
  • For a loop intended to finish, identify progress or the event it awaits; check that a misplaced update or reset does not prevent the exit.

What’s next

Every box so far holds one value. In the next module, Structure, a box holds many of them at once, which is how a program keeps a high score table, a row of enemies or a level.