🚀 Alguni Start learning

Unit 5: Decisions

Programs that look before they leap.

Unit 5 of 8 in Robotics coding for kids. Its 4 lessons are If, Else, Three Ways and The Junction — below is everything each one explains, and a question or two from it to try.

Every program on this page was driven round the simulated room before it shipped, and prints exactly what it says it prints.

This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.

🤔 If

Only if

if runs the indented lines only when something is true. If it is not true, they are skipped and the program carries on underneath.

Here the block is 62 cm away, which is less than 100 — so the warning gets said.

Python

if robot.distance() < 100:
    robot.say("Something is close")

robot.say(robot.distance())

It prints

Something is close
62.0

Try it yourself

What happens to the indented lines when the if is not true?

  • They are skipped, and the program carries on below them
  • The program stops
  • They run anyway, just later
  • The robot bumps

Nothing is close here. What does this print?

Python

if robot.distance() < 100:
    robot.say("Something is close")

robot.say("Done")

Answer them in the app

↔️ Else

One or the other, never both

else is the other road. Exactly one of the two blocks runs, every single time — so the robot always does something.

This is how a robot chooses which way to go round an obstacle: measure both sides, take the roomier one.

Python

if robot.distance_left() > robot.distance_right():
    robot.say("more room on the left")
else:
    robot.say("more room on the right")

It prints

more room on the right

Try it yourself

What does this print?

Python

if robot.distance_left() < 40:
    robot.say("wall on the left")
else:
    robot.say("nothing on the left")

How many of the two blocks in an if / else run?

  • Exactly one, always
  • Both
  • Neither, unless the test is true
  • It depends on the robot

Answer them in the app

🔀 Three Ways

elif: as many roads as you like

elif means "otherwise, if…". Python tries each test in turn and takes the first one that is true, then skips all the rest.

Order matters. A test for "less than 100" put first would swallow everything a "less than 30" test was meant to catch.

Python

gap = robot.distance()

if gap < 15:
    robot.say("stop")
elif gap < 60:
    robot.say("slow down")
else:
    robot.say("full speed")

It prints

slow down

Try it yourself

The wall ahead is a long way off. What does this print?

Python

gap = robot.distance()

if gap < 15:
    robot.say("stop")
elif gap < 60:
    robot.say("slow down")
else:
    robot.say("full speed")

If two elif tests are both true, which one runs?

  • The first one written
  • The last one written
  • Both of them
  • Neither

Answer them in the app

🏆 The Junction

Try it yourself

Why is a program that measures better than one that just turns left?

  • Because it still works when the room is not the one you expected
  • Because measuring is faster
  • Because turning left is broken
  • It is not better, only longer

Answer it in the app