NEBians
search Sign In Register

Smart Choices

Lesson 3 of 3 Coding game schedule20 min

Loading simulation…

sports_esportsPlay with the blocks and run your code

flagWhat you'll discover

  • arrow_forwardUse the if-path-ahead block to run blocks only when the way is clear
  • arrow_forwardProtect the robot from crashes with a conditional safety check
  • arrow_forwardCombine repeat and if blocks into one smart pattern
  • arrow_forwardSolve a maze your blocks were never measured for

Programs that decide

So far your robot has been brave but blind — it moves even when a wall is in the way. The if-path-ahead block changes everything: blocks inside it only run when the path in front of the robot is clear. This is called a conditional, and it is how programs make decisions. Your phone uses conditionals millions of times a day: IF the battery is low, dim the screen. IF the password matches, unlock. IF a message arrives, buzz.

Look before you leap

Put a move block inside an if-path-ahead block and the robot becomes crash-proof: it checks the square ahead, moves when it is open, and calmly does nothing when it is blocked. That means a loop can safely run "too many" times — extra rounds simply do nothing once the robot reaches a wall. This is a real engineering idea called a guard: code that checks conditions before acting. Guards are why a well-built app refuses to crash even when something unexpected happens.

One pattern, any maze

Here is the magic of combining your three powers. The pattern "repeat: walk while the path is clear, then turn right" follows walls around corners without knowing the maze in advance! Sequences, loops and conditionals are the three building blocks of all software — from games to rockets. Computer scientists proved that any program, no matter how complex, can be built from just these three structures. Master them and you understand the skeleton of every app you have ever used.

quizCheck your knowledge

1. What does the if-path-ahead block do?
2. A move block sits inside if-path-ahead, and a wall is directly ahead. What happens?
3. Which three structures can build any program ever written?