Skip to content

Asking Two Things at Once

Join questions together with AND, OR and NOT, so one test can ask about more than one thing.

Real questions are rarely about one thing. Can the player shoot? Only if they are alive and they have ammo. Is that a valid guess? Only if it is at least 1 and at most 10.

You can join questions together.

Both, or either

AND is true when both halves are true. OR is true when either half is, or both.

ASK "Guess a number from 1 to 10? " INTO guess
IF guess < 1 OR guess > 10 THEN
  SHOW "That is not between 1 and 10"
ELSE
  SHOW "Good guess"
END
Output
Guess a number from 1 to 10? 42
That is not between 1 and 10

That OR catches a guess that is too small and a guess that is too big, in one question. Written with AND it says the same thing from the other side:

IF guess >= 1 AND guess <= 10 THEN
  SHOW "Good guess"
ELSE
  SHOW "That is not between 1 and 10"
END

>= means at least, and <= means at most. They save you writing guess > 0 when what you mean is guess >= 1.

The opposite

NOT turns an answer round. NOT yes is no, and NOT no is yes.

IF NOT (guess = 7) THEN
  SHOW "Keep going"
END

The brackets are doing the job they did in Working It Out: work this out first. NOT turns round whatever sits inside them, so the whole question guess = 7 gets answered, and then turned round.

That asks whether the guess is anything other than seven. You could write the same thing as guess <> 7, which means is not equal to. Use whichever reads better where you are standing: NOT in front of a whole question, <> inside a small one.

Two questions, not one twice

A trap worth meeting now. This does not do what it looks like:

IF guess = 3 OR 7 THEN

Read it as the computer does. It joins two questions: is guess equal to 3, and is 7. The second one is not a question at all. Say it in full:

IF guess = 3 OR guess = 7 THEN

Both halves of an AND or an OR have to stand on their own as questions.

Mixing them

AND and OR have an order too, the way * and + do: AND goes first. So alive AND ammo > 0 OR grenades > 0 may not ask what you think it asks.

Rather than lean on the rule, say it with brackets:

IF alive AND (ammo > 0 OR grenades > 0) THEN
  SHOW "You can shoot"
END

Now nobody has to work out which half binds tighter — you have written it down.

When it’s wrong, see why

  • The test is always true. Check an OR that cannot come back no. `guess < 1 OR guess

    0` covers every number there is.

  • The test is never true. Check an AND asking for two things that cannot both hold. guess < 1 AND guess > 10 has no answer.
  • You wrote the value once and the question once. guess = 3 OR 7 names a value where a question belongs. Write both questions out.

What you’ve learnt

  • AND is true when both halves are true. OR is true when either is.
  • NOT turns an answer round, and <> asks whether two things differ.
  • >= and <= ask at least and at most.
  • Each half of a joined question has to be a question in its own right.

What’s next

Your programs can decide, and now they can decide about more than one thing at a time. In the next module, Repeating, you stop writing the same line ten times over.