We use ordinary numeric guesses in this unit. Assume ASK has supplied a valid number; checking typed text or rejecting an invalid answer is extra behaviour. Here are the three separate tests from the guessing example:
IF guess < 7 THEN SHOW "Too low"
IF guess > 7 THEN SHOW "Too high"
IF guess = 7 THEN SHOW "Correct!"The computer runs all three, every time. It asks whether the guess is low, then whether it is high, then whether it is right. Only one of them can be true, but nothing in the program says so. Those three questions do not know about each other.
Do this, or else do that
ELSE gives one question two paths.
ASK "Guess my number? " INTO guess
IF guess = 7 THEN
SHOW "Correct!"
ELSE
SHOW "Not this time"
ENDGuess my number? 4 Not this time
Read it aloud and it says what it does: if the guess is seven, show Correct!, or else
show Not this time. Exactly one of those two lines runs. Never both, and never neither.
The END marks where the decision finishes and the program carries on.
More than two answers
A guess can be low, high or right. That is three answers, not two. Hang another question
off the ELSE:
ASK "Guess my number? " INTO guess
IF guess < 7 THEN
SHOW "Too low"
ELSE IF guess > 7 THEN
SHOW "Too high"
ELSE
SHOW "Correct!"
ENDGuess my number? 4 Too low
The computer works down the chain and selects the first question that comes back yes. A
low guess shows Too low and skips the rest. Otherwise it tries the next question. The
last ELSE catches everything left over, which here is the one case where a guess is
neither low nor high.
Compare that with the three separate tests at the top of this unit. Same three messages, but the chain says something the separate tests only implied: these are three answers to one question, and exactly one of them happens.
When two questions can both be true
Our guessing tests cannot both be true for the same unchanged number. Other conditions overlap. Suppose scores are whole numbers from 0 to 100:
IF score >= 80 THEN
SHOW "High score"
ELSE IF score >= 50 THEN
SHOW "Pass"
ELSE
SHOW "Keep trying"
END
SHOW "Done">= means greater than or equal to. At 80, both comparisons would be true,
but the first branch wins. Once that branch finishes, we skip the later branches
and carry on after END: Done still appears. We have not stopped the program.
| Score | Chosen message | Next output |
|---|---|---|
| 49 | Keep trying | Done |
| 50 | Pass | Done |
| 79 | Pass | Done |
| 80 | High score | Done |
| 81 | High score | Done |
If we put score >= 50 first, which message does a score of 80 choose? Would two separate IF tests behave the same way?
Show the explanation
The broad first test chooses Pass, so the High score branch is never reached. In that order, every score qualifying for High score has already matched Pass. Two separate IF tests would both run at 80 and could show both messages. Choose an order and structure that express the intended rule; neither structure is always right. Done still follows the chain.
When it’s wrong, see why
- Two messages appear. You have separate
IFs where you wanted a chain. EachIFasks its own question, so more than one can come back yes. - Nothing appears at all. If no condition matches and there is no final ELSE, no branch runs. That can be intentional. Add a final ELSE when the intended rule requires a fallback message.
- The wrong branch runs for a value on the boundary. Check which test claims the edge.
guess < 7andguess > 7both leave7out, which is what lets the lastELSEcatch it.
What you’ve learnt
ELSEgives a question a second path, and exactly one path always runs.ELSE IFselects the first matching branch; execution continues after END.- A last
ELSEcatches everything the questions above it did not. - A chain says that these answers belong to one question. Separate
IFs do not.
What’s next
Every question so far is answered and forgotten in the same breath. In Unit 3 you keep the answer, which turns out to be how a game knows it is over.