So far, a program can give different answers from different input values. A greeting uses the name you type; a sum uses the numbers. Those examples still carry out the same instructions in the same order.
Now we will make a program carry out an instruction only when a condition is true. The computer will choose whether to do that step.
Do it only if
A decision is a question with a yes-or-no answer, and an instruction to carry out only if the answer is yes.
ASK "Guess my number? " INTO guess
IF guess = 7 THEN SHOW "Correct!"The second line asks a question: is guess equal to 7? It runs the SHOW only when the
answer comes back yes.
Guess 7:
Guess my number? 7 Correct!
Guess anything else, and nothing happens at all:
Guess my number? 4
IF … THEN. When the answer is yes the program carries straight on through the message. When it is no, it steps around it. There is no second path here: just a step that either happens or does not.That is a condition: a question with two possible answers. When the answer is yes, the
instruction after THEN happens. When it is no, the program steps around it and carries
on.
Notice that = is doing a second job here. In LET guess = 7 it means put 7 in the
box. In IF guess = 7 it asks are these two the same? One symbol, two jobs, and the
computer tells them apart by where it sits.
Asking other questions
= is not the only question you can ask. < means less than, and > means greater than.
ASK "Guess my number? " INTO guess
IF guess < 7 THEN SHOW "Too low"
IF guess > 7 THEN SHOW "Too high"Guess my number? 4 Too low
The same questions work on text. IF name = "Sam" asks whether the box holds exactly
that, letter for letter, and a capital S is not the same character as a small one.
Three tests, and only the one that is true does anything. That is already a tiny game: a secret number, a guess, and an answer that depends on the guess. It just cannot let you try again yet.
When it’s wrong, see why
- The reaction never happens. The test is never true. Check it against the value you
typed. If you guess
7and get nothing, read the test again: is it looking at the box you put the guess in? - Two reactions happen at once. Two of your tests can be true together.
guess < 7andguess < 10are both true for4. Tests that share nothing, like<,=and>, give you one answer. - The message appears for every value you try. Check the condition against values
on both sides of its boundary, including the boundary itself.
guess > 0is true for a positive number, but false for zero or a negative number. Trying only positive numbers would not show you the other path. Asking for a positive number does not stop someone typing zero; checking input is a separate job.
Check the boundary
Predict whether SHOW runs for each of these values of guess: -1, 0 and 1.
Does the test change the value stored in guess?
IF guess > 0 THEN SHOW "Positive"Check the three cases
| guess | guess > 0 |
|---|---|
| -1 | FALSE |
| 0 | FALSE |
| 1 | TRUE |
Only 1 makes this example show Positive. The comparison reads guess; it does
not change it. If a program has already checked that only positive numbers can
reach this line, the condition will always be true there. Without that check, it
still has a false case.
What you’ve learnt
- A condition is a question with a yes-or-no answer.
IFcarries out its instruction only when the answer is yes, and skips it otherwise.- The questions you can ask are
=,<and>. =as a question is a different job from=as put this in the box.- A decision is what lets a program react, which is the first thing that makes it feel like a game.
What’s next
Each of those tests stands on its own, and the three of them do not know about each other. In Unit 2 the program chooses between answers instead of testing for each one separately.