Skip to content

A Yes You Can Keep

The answer to a question is a value. Put it in a box and the program can remember it, carry it about, and act on it later.

Every question an IF asks has a yes or a no for an answer. So far the program gets the answer, acts on it, and forgets it in the same breath.

You can keep it. A yes or a no is a value, just as 7 and "Sam" are, and a box can hold one.

TRUE and FALSE

The two values are written TRUE and FALSE. Put one in a box, and the box becomes a question you have already answered:

LET alive = TRUE
IF alive THEN SHOW "Still going"
Output
Still going

IF alive asks nothing new. It looks in the box, finds a yes, and carries on. There is no = in that test because there is nothing to compare: the box already holds the answer.

A box that holds a yes or a no is called a flag, and every game has a handful of them. alive. paused. gameOver. Each one is a fact about the game, kept where any part of the program can check it.

Keeping the answer to a question

You can put a question’s answer straight into a box:

ASK "Guess my number? " INTO guess
LET tooLow = guess < 7
IF tooLow THEN SHOW "Too low"
Output
Guess my number? 4
Too low

guess < 7 is a question, and its answer is TRUE or FALSE. LET tooLow = ... puts that answer in a box, the same way LET total = a + b puts a sum in one. Then IF tooLow reads it back.

Why bother, when IF guess < 7 would have done? Because the question is now named. A line that says tooLow tells you what it is checking. A line that says guess < 7 makes you work it out.

Raising a flag

The real use is carrying an answer from the place it was found to the place it is needed.

LET gameOver = FALSE
LET lives = 0
IF lives = 0 THEN LET gameOver = TRUE
IF gameOver THEN SHOW "Game over"
Output
Game over

Line 3 finds out the game is over. Line 4 acts on it. In a real game those two lines are far apart: one deep inside the part that handles being hit, the other at the top of the loop that decides whether to keep playing. The flag is how the news travels between them.

Set it FALSE to start, so the box holds a sensible answer before anything has happened. Then let events raise it.

When it’s wrong, see why

  • IF alive = TRUE. That works, but it asks whether a yes is a yes. IF alive says the same thing without the detour.
  • The flag never comes down. Something set it TRUE and nothing sets it back. A flag that only ever goes up is a one-way door.
  • You put "yes" in the box. That is text, not an answer. IF alive wants TRUE or FALSE, and "yes" is neither.
  • The flag was never given a starting value. A box you have not filled holds nothing, which is not a no. Set every flag before the first line that reads it.

What you’ve learnt

  • The answer to a question is a value, TRUE or FALSE, and a box can hold one.
  • A box holding one is a flag, and IF alive reads it without needing =.
  • You can put a question’s answer in a box, which gives the question a name.
  • A flag carries an answer from where it was found to where it is needed.

What’s next

Real questions are rarely about one thing. In Unit 4 you join questions together, and flags turn out to fit into a joined question exactly as a comparison does.