Code & Robots, level 4
🧱 If There's a Wall
How does a robot know when to turn?
- How does a robot know when to turn? Coders give it a rule called a conditional, which always starts with the word if. It might say: if there is a wall ahead, turn. Ahead means the next square in the same direction the robot faces. The robot checks first, and then it decides what to do, so it never bumps into the wall.
- A conditional asks a question that is either true or false. Is there a wall ahead? If the answer is true, the robot turns. If the answer is false, the robot does something different instead, and coders write that part with the word else. So the whole rule says: if there is a wall ahead, turn; else, go forward.
- You use rules like this every day. If it is raining, wear a coat; else, wear a T-shirt. If the cup is full, stop pouring; else, keep pouring. Each rule checks something first. When you put a conditional inside a loop, a robot can find its own way, because it checks for a wall at every single step.
Try it at home: Play an if-else game: if I clap, you jump; else, you stand still. Take turns making up the rule.
Questions and answers
If There's a Wall
- 🤔 A conditional starts with which word? if
- 🧱 Is there a wall ahead? The answer to that question is… true or false (A conditional's question is always either true or false.)
- ☀️ If it is raining, wear a coat; else, wear a T-shirt. It is sunny. What do you wear? A T-shirt (It is not raining, so you do the else part.)
- When the answer to the if question is false, the robot does the… else part
- 🤖 Rule: if there is a wall ahead, turn. There is a wall ahead. What does the robot do? Turn (The question is true, so the robot does the if part and turns.)
- 🥛 If the cup is full, stop pouring; else, keep pouring. The cup is half full. What do you do? Keep pouring (Half full is not full, so you do the else part.)