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"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. Games often use them.
alive. paused. gameOver. Each holds a recorded answer that other instructions can read where that
name is available. A named job may have its own local names.
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"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.
A saved answer does not ask again
LET tooLow = guess < 7 calculates the comparison now and stores its answer.
It does not store a rule that automatically follows later changes to guess:
LET guess = 4
LET tooLow = guess < 7
LET guess = 9| Step | guess | tooLow |
|---|---|---|
| Set guess to 4 | 4 | not assigned |
| Save guess < 7 | 4 | TRUE |
| Set guess to 9 | 9 | TRUE |
| Assign tooLow = guess < 7 again | 9 | FALSE |
The last row requires another assignment. Changing guess alone does not perform it. If we need the current answer, compare guess again or update the saved flag before using it. If we want the earlier answer, keeping it unchanged is useful.
After the three code lines, is IF tooLow true or false? What about a fresh test of guess < 7?
Show the explanation
IF tooLow is true: the box still holds the answer calculated when guess was 4. A fresh test of guess < 7 is false because guess is now 9. Assigning the new comparison result to tooLow makes both tests agree again.
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"Game over
Line 3 finds out the game is over. Line 4 acts on it. In a game those jobs may be 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. This is a latched flag: once set, it remembers
that the event happened, rather than recalculating whether lives is zero.
Starting a new game
After the example above, gameOver is TRUE. Setting lives to 3 does not change that stored answer. A new game must reset the state its rules need:
LET lives = 3
LET gameOver = FALSEIf a restart restores three lives but omits gameOver = FALSE, does IF gameOver still choose Game over?
Show the explanation
Yes. Restoring lives alone leaves gameOver TRUE. Resetting it to FALSE makes the new game’s starting state agree with its intended rule. Keeping a flag set can be deliberate within one game; carrying that state into a new game may be a bug. The reset belongs at the new-game boundary, not on every update where it would erase the event before another part could use it.
When it’s wrong, see why
IF alive = TRUE. That works, but it asks whether a yes is a yes.IF alivesays the same thing without the detour.- The flag never comes down. Check its intended meaning. An event latch can stay TRUE until handled or reset; a current-condition flag needs refreshing. Include the explicit reset when starting a new game.
- You put
"yes"in the box. That is text, not an answer.IF alivewantsTRUEorFALSE, 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,
TRUEorFALSE, and a box can hold one. - A box holding one is a flag, and
IF alivereads 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.