Skip to content

Making a Choice

A program that decides: test a value, and do something only if the test passes.

Everything so far runs the same way every time, whatever the values are. The program cannot react to anything.

This unit changes that. The computer learns to decide.

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:

Output
Guess my number? 7
Correct!

Guess anything else, and nothing happens at all:

Output
Guess my number? 4
noASK for a guessguess = 7 ?SHOW "Correct!"carry on
The shape of 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"
Output
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 7 and 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 < 7 and guess < 10 are both true for 4. Tests that share nothing, like <, = and >, give you one answer.
  • It reacts whatever you type. Look for a test that cannot come back no, like guess > 0 when nobody would guess a negative number. A question with only one possible answer is not a decision.

What you’ve learnt

  • A condition is a question with a yes-or-no answer.
  • IF carries 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.