Session 345 minutes

Session 3: Conditional Branching (If-Then-Else) & Collision Detection

Learning objective: Empowering your robot with decision-making logic using sensing and conditionals.

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 ... then and if ... 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.

If the condition is true → do the action.

2. True and false conditions

A condition has only two possible results:

ResultMeaning
TrueRun the blocks inside
FalseSkip them, or run the else branch

Some common conditions in PictoBlox:

text
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.

text
if <touching [Wall]?> then
    play sound [Alarm]
    move -10 steps

If the sprite is not touching the wall, the blocks inside are skipped.

Mục Trọng Tâm

Example: keyboard control

text
when green flag clicked
forever
    if <key [right arrow] pressed?> then
        change x by 5

The 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.

text
if <touching [Wall]?> then
    play sound [Alarm]
    move -10 steps
    turn right 90 degrees
else
    move 5 steps

How the program works:

  1. 1Check whether the sprite is touching the wall.
  2. 2If yes, the sprite warns, backs up and turns.
  3. 3If not, the sprite keeps moving forward.
ConditionAction
Touching the wallWarn, back up and turn
Not touching the wallKeep going forward

5. Combining conditions with sensors

A robot can use an ultrasonic sensor to measure the distance to an obstacle.

text
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 5

In this program:

  • Distance < 15 is the condition.
  • When an obstacle is closer than 15 cm, the robot avoids it.
  • When the way ahead is clear, the robot keeps moving.
⚠️ When testing with a real robot, use a low speed and place it on a wide, flat surface.

6. Combining several conditions

Sometimes a robot needs to check several things at once.

Mục Trọng Tâm

The and condition

Both conditions must be true.

text
if <Distance less than 15 and Speed greater than 0> then
    stop motors
Mục Trọng Tâm

The or condition

Only one of the two conditions needs to be true.

text
if <touching edge or touching color red> then
    turn around
Mục Trọng Tâm

The not condition

Reverses the result of a condition.

text
if <not touching [Wall]> then
    keep moving

7. Practice: the path-finding sprite

Mục Trọng Tâm

Task

Program the sprite to:

  1. 1Keep moving forward.
  2. 2Check whether it is touching a wall.
  3. 3If it touches a wall, back up and change direction.
  4. 4If not, keep going forward.
Mục Trọng Tâm

Suggested program

text
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] steps

Try changing the turn from 90 degrees to 45 degrees. How does the sprite's path change?

8. Common mistakes

Mục Trọng Tâm

Not putting the condition inside a loop

If you only check once, the robot stops observing its surroundings.

Use:

text
forever
    if <condition> then
        do the action
Mục Trọng Tâm

Mixing up comparison signs

text
Distance less than 15

means the obstacle is close.

text
Distance greater than 15

means there is space ahead.

Mục Trọng Tâm

The robot keeps spinning on the spot

After meeting an obstacle, the robot should back away before turning:

text
stop motors
reverse
turn right

🧩 Challenge

Build a robot with three levels of reaction:

text
if <Distance less than 10> then
    stop and reverse
else
    if <Distance less than 25> then
        slow down
    else
        drive normally

The 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 True or False.
  • if ... then runs actions when the condition is true.
  • if ... then ... else chooses 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.
Mục Trọng Tâm

Quick questions

  1. 1What results can a condition have?
  2. 2How is if ... then different from if ... then ... else?
  3. 3Why should sensor checks go inside a loop?
  4. 4What should the robot do when an obstacle is closer than the safe distance?

Ready to test your knowledge?

Open the dedicated exam room to answer questions and review your results.

Part 1 Mastery Quiz: Scratch Logic & Core Blocks →14 questions · 15 minutes