1 / 24
Programming · Grade 6 · Chapter 4

Scratch: Loops & Conditionals

Mastering iteration structures, decision trees, collision sensing, and boolean logic in Scratch.

Lesson Overview

Learning Objectives

Algorithmic Thinking

Why Sequential Code Is Not Enough

Linear Sequence (Limited)

Code executes line 1, then line 2, then line 3, then stops. Cannot repeat actions or adapt to player input dynamically.

Control Structures (Dynamic)

Loops keep scripts running continuously; Conditionals evaluate live game events (key presses, wall hits, timer limits).

Debugging Case Study

The Ghost Walking Through Walls

Zain built a maze game. He wrote: when green flag clickedif touching [Wall] then turn 180 degrees. But when the green flag is clicked, his cat walks right through the maze wall!

Smartboard Challenge: Why did the if block run only once at millisecond 0 and then shut off forever?
Iteration Mechanics

The 3 Scratch Loop Typologies

COUNT-CONTROLLED

repeat (10)

Executes enclosed blocks a fixed number of times, then moves to the next block underneath.

INFINITE LOOP

forever

Repeats enclosed blocks continuously until the Stop sign is pressed or stop all is called.

CONDITION-CONTROLLED

repeat until <>

Keeps looping until a specific Boolean condition becomes TRUE.

Decision Making

Branching: if-then vs if-then-else

if <condition> then

Single branch: If condition is true, execute inner blocks. If false, skip inner blocks completely.

if <condition> then ... else

Dual branch: Guaranteed action! Does Branch A if true, or Branch B if false.

Control Flow

Flowchart of if-then-else Execution

STEP 1

Evaluate Condition

Check Boolean: e.g. <touching [Enemy]?>

STEP 2A

If TRUE

Execute Branch A: Loose 1 Life & Reset Position.

STEP 2B

If FALSE

Execute Branch B: Keep walking forward.

STEP 3

Rejoin Script

Continue to next block outside conditional.

Inputs & Triggers

Sensing Blocks & Boolean Conditions

Hexagonal blocks return either TRUE or FALSE.

Sensing Touch

<touching [Sprite]?>
<touching color [#FF0000]?>

Sensing Keys

<key [space] pressed?>
<key [up arrow] pressed?>

Comparison Operators

<(score) > 50>
<(timer) = 0>

Logic Gates Connection

Scratch Operators = Logic Gates!

The green operator blocks in Scratch perform the exact same Boolean operations as Chapter 2 hardware logic gates.

AND GATE

< () and () >

True ONLY if both condition A and condition B are met.

OR GATE

< () or () >

True if at least one condition is met.

NOT GATE

< not () >

Flips True to False, and False to True.

Game Architecture

Smooth Keyboard Controller Script

when green flag clicked forever { if <key [right arrow] pressed?> then { change x by 10 } if <key [left arrow] pressed?> then { change x by -10 } }
Game Physics

Collision Detection & Wall Bouncing

when green flag clicked forever { move 5 steps if <touching [Maze Wall] ?> then { // Bounce back to undo step! move -5 steps } }
Optimization

touching sprite vs touching color

touching [Sprite]?

Fast and reliable! Works regardless of background colors or stage graphics changes.

⚠️ touching color [#00FF00]?

Can fail if backdrop shade changes slightly or color picker is off by 1% hue!

Advanced Mechanics

Nested Loops (Loop Inside Loop)

Useful for grid generation, repeating animation cycles inside continuous game loops.

repeat 4 { // Outer loop: 4 sides of a square repeat 10 { // Inner loop: walk 10 mini-steps move 5 steps } turn right 90 degrees }
Myth Buster

The "Single Execution" Trap

❌ Missing Forever Loop

Placing if key space pressed directly under green flag. It checks key press in 0.001s and ends!

✅ Wrapped in Forever

Wrapping the if block inside a forever loop ensures it checks key presses 60 times every second!

Smartboard Challenge

Trace the Loop Variable

A sprite starts with X = 0.

Code: repeat 4 { change X by 15 }

Solve on smartboard: What is the final X value after the loop finishes executing?
Smartboard Challenge

Predict the Condition Outcome

Given: Score = 15 | Lives = 0

Condition A: <<(Score) > 10> and <(Lives) > 0>>

Condition B: <<(Score) > 10> or <(Lives) > 0>>

Class Vote: Is Condition A True or False? Is Condition B True or False? Explain why!
Game Design Challenge

Designing an Enemy Sentry Guard

An enemy sprite patrols left and right until it detects the player sprite, then plays a warning alarm.

repeat until <touching [Player] ?> { move 10 steps if on edge, bounce } play sound [Alert] until done
Guided Coding

Building a Coin Collector Trap

Build a coin that glides around, giving points when collected, but disappears if the bomb touches it.

forever { if <touching [Player] ?> then { play sound [Coin] go to random position } }
Spot The Bug

Find the Bug in this Jump Routine

when green flag clicked if <key [space] pressed?> then { repeat 10 { change y by 10 } repeat 10 { change y by -10 } }
Debug Prompt: Why does pressing spacebar during the game NOT make the sprite jump?
Interactive Quiz · Question 1

Which Scratch loop executes enclosed blocks indefinitely until manually stopped?

Arepeat (10)
Bforever
Crepeat until
Dif-then-else
Click to reveal answer
Interactive Quiz · Question 2

What happens if an if key space pressed block is NOT inside a forever loop?

AIt causes Scratch to crash
BIt only checks spacebar at the exact instant the green flag is clicked
CIt automatically loops forever anyway
DIt turns into a hat block
Click to reveal answer
Interactive Quiz · Question 3

Which logical block returns TRUE if Condition A is True AND Condition B is False?

A< (A) and (B) >
B< (A) or (B) >
C< not (A) >
DNone of the above
Click to reveal answer
Lesson Recap

Summary of Key Takeaways

Exit Ticket

Before You Leave

In your notebook, sketch a flowchart for:

A character moving forward continuously, but IF touching a spike sprite, THEN play "ouch" sound and go back to (0,0), ELSE change costume to walk.

Next Chapter: Scratch Variables & Scoreboard Game Mechanics!