Session 3: Conditional Branching (If-Then-Else) & Collision Detection
Theory & practice
🧠 Lesson 3: Logical Thinking — If, Then and Else
🎯 Learning objectives
After this lesson, you will be able to:
- Understand true and false conditions in programming.
- Tell the difference between
if ... thenandif ... then ... else. - Use sensors so a sprite or robot can make decisions.
- Combine conditions with loops to build smart programs.
1. Smart robots make decisions
A smart robot doesn't just move in one direction. It needs to observe its surroundings and choose the right action.
For example:
- If there is an obstacle ahead, then the robot stops.
- If it gets dark, then turn on the lights.
- If the right arrow key is pressed, then the sprite moves right.
- If not — there is no obstacle — the robot keeps going forward.
This way of thinking is called conditional thinking.
2. True and false conditions
A condition has only two possible results:
| Result | Meaning |
|---|---|
True | Run the blocks inside |
False | Skip them, or run the else branch |
Some common conditions in PictoBlox:
touching [Wall]?
key [right arrow] pressed?
distance less than 15 cm?
Score greater than 10?
color red is touching blue?Condition blocks are usually hexagon-shaped and fit into the slot of the if block.
3. The if ... then block
This block runs its actions only when the condition is true.
if <touching [Wall]?> then
play sound [Alarm]
move -10 stepsIf the sprite is not touching the wall, the blocks inside are skipped.
Example: keyboard control
when green flag clicked
forever
if <key [right arrow] pressed?> then
change x by 5The forever loop lets the program check the keyboard many times every second.
4. The if ... then ... else block
This block lets the program choose one of two actions.
if <touching [Wall]?> then
play sound [Alarm]
move -10 steps
turn right 90 degrees
else
move 5 stepsHow the program works:
- 1Check whether the sprite is touching the wall.
- 2If yes, the sprite warns, backs up and turns.
- 3If not, the sprite keeps moving forward.
| Condition | Action |
|---|---|
| Touching the wall | Warn, back up and turn |
| Not touching the wall | Keep going forward |
5. Combining conditions with sensors
A robot can use an ultrasonic sensor to measure the distance to an obstacle.
when green flag clicked
forever
set [Distance] to [ultrasonic sensor value]
if <Distance less than 15> then
stop motors
play sound [Alarm]
reverse for 1 second
turn right 90 degrees
else
drive forward at speed 5In this program:
Distance < 15is the condition.- When an obstacle is closer than 15 cm, the robot avoids it.
- When the way ahead is clear, the robot keeps moving.
6. Combining several conditions
Sometimes a robot needs to check several things at once.
The and condition
Both conditions must be true.
if <Distance less than 15 and Speed greater than 0> then
stop motorsThe or condition
Only one of the two conditions needs to be true.
if <touching edge or touching color red> then
turn aroundThe not condition
Reverses the result of a condition.
if <not touching [Wall]> then
keep moving7. Practice: the path-finding sprite
Task
Program the sprite to:
- 1Keep moving forward.
- 2Check whether it is touching a wall.
- 3If it touches a wall, back up and change direction.
- 4If not, keep going forward.
Suggested program
when green flag clicked
set [Speed] to [5]
forever
if <touching [Wall]?> then
play sound [Alarm]
move -10 steps
turn right 90 degrees
wait 0.2 seconds
else
move [Speed] stepsTry changing the turn from 90 degrees to 45 degrees. How does the sprite's path change?
8. Common mistakes
Not putting the condition inside a loop
If you only check once, the robot stops observing its surroundings.
Use:
forever
if <condition> then
do the actionMixing up comparison signs
Distance less than 15means the obstacle is close.
Distance greater than 15means there is space ahead.
The robot keeps spinning on the spot
After meeting an obstacle, the robot should back away before turning:
stop motors
reverse
turn right🧩 Challenge
Build a robot with three levels of reaction:
if <Distance less than 10> then
stop and reverse
else
if <Distance less than 25> then
slow down
else
drive normallyThe robot will:
- Stop when the obstacle is too close.
- Slow down as it gets closer to the obstacle.
- Drive normally when the way ahead is clear.
✅ Remember
- A condition is either
TrueorFalse. if ... thenruns actions when the condition is true.if ... then ... elsechooses between two actions.- Loops let the robot check conditions continuously.
- Sensors give the robot information to make decisions.
- The robot should stop or back up before changing direction.
Quick questions
- 1What results can a condition have?
- 2How is
if ... thendifferent fromif ... then ... else? - 3Why should sensor checks go inside a loop?
- 4What should the robot do when an obstacle is closer than the safe distance?