Level 1Unit B · Session 8
8

Unit B · Telling a computer what to do

If this, then that

What we are making: a program that behaves differently depending on what is happening — the first time your program makes a decision instead of just obeying.

The idea

Everything so far runs the same way every time. Real programs do not. They look at something, and then choose.

The block is called if. It asks a question that can only be answered yes or no, and it runs what is inside it only when the answer is yes.

Runs only when the answer is yes
if touching edge ? then
say Ouch! for 1 seconds

If the cat is not touching the edge, the say block is skipped entirely, as if it were not there. Then the program carries on.

📷
images/l1-s8-palette-sensing.png
Screenshot: the light-blue Sensing palette, with touching? and key pressed? pointed out
New words
ConditionThe question. It is either true or false, never maybe.
True / FalseThe only two answers a condition can give.
IfDo this, but only when the condition is true.
If / elseDo this when true, that when false. Always one or the other.

Do it — one rule

The if block needs to be checked over and over, so it lives inside a forever loop. That pairing — forever plus if — is the shape of almost every program you will write this year, including the robot ones in Unit C.

Check, decide, act — over and over
when green flag clicked
forever
move 4 steps
if touching edge ? then
turn 180 degrees
📷
images/l1-s8-script-forever-if.png
Screenshot: forever with an if inside it — the shape you will use all year
  1. Build it and run it. The cat walks to the edge, turns round, walks back.
  2. Take the if block out of the forever loop and put it underneath. Run it. What happens now, and why?
  3. Put it back inside.
  4. Change move 4 to move 20. Does it still turn reliably? Why might it not?
⚠ Careful
If the check is outside the loop it happens once, at the very start, when the cat is not touching anything. A check that happens once is not a rule — it is a single glance.

Do it — two answers

If / else has two arms. One of them always runs.

One arm always runs
when green flag clicked
forever
if key space pressed ? then
say Going!
move 10 steps
else
say Waiting.
📷
images/l1-s8-script-if-else.png
Screenshot: the if / else block with something in both arms
  1. Build it. Run it. Hold the space bar down, then let go.
  2. Swap the two arms over so it moves when space is not pressed. Run it.
  3. Add a second if inside the first one. Make it only move when space is pressed and it is not touching the edge.

Try it

  1. Make the cat change colour when it touches the edge, and stay the same otherwise.
  2. Make it say a different thing for three different keys.
  3. Make a sprite that follows the mouse pointer but stops when it gets close. You will need distance to mouse-pointer from the sensing group.
What you seeWhyWhat to do
The if never runsIt is not inside a forever loop.A check has to happen over and over to be useful.
It runs constantly, not onceThat is correct behaviour for a forever loop.If you only want it once, use 'wait until' instead of 'if'.
Nothing in the else arm ever happensThe condition is always true.Test the condition on its own first — click the block to see true or false.
It flickers between the twoThe condition is right on the boundary.You will meet this properly in Level 2. For now, move the boundary.

What you learned

  • An if block asks a yes-or-no question and acts on the answer.
  • A condition is true or false. Never anything else.
  • Checks belong inside a forever loop, or they only happen once.
  • If / else always runs exactly one of its two arms.
  • Forever plus if is the shape of nearly every useful program.
Challenge optional — only if you finish early
  • Make a sprite that says something different depending on whether it is touching the edge, touching another sprite, or neither. Three answers, not two.
7. Repeat and forever