Part 1 — What this chapter teaches: the Student Learning Outcomes and 4.1 Introduction (printed pp 92–93)
The chapter opens with its Student Learning Outcomes (printed p 92). By the end of this unit, students will be able to:
- Understand general control structures and their uses (sequence, loops, and conditions).
- Use simple and nested loops in computer programs.
- Tell the difference between 'repeat', 'forever', and 'repeat until' loops.
- Apply simple as well as nested conditions in computer programs.
- Differentiate between if and if else conditions.
- Discuss the concept of functions in a computer program.
- Use simple functions in a computer program.
- Discuss the concept of cloning in a computer program.
- Design high-level games like tic-tac-toe, maze with multiple levels, etc. (Group Project).
4.1 Introduction. This chapter takes you beyond the basics: you will use intermediate constructs — functions, cloning and conditional movement — and learn them by building mini-games in a visual programming tool, Scratch.
🌱 Extra Help Words — Words in this part
instruction — One single step that tells the computer what to do; a program is a list of instructions in a careful order.
Part 2 — 4.2 Visual versus Textual Programming (printed p 93)
Programs can be written in two ways. In textual programming you type code in a written language. In visual programming you drag picture blocks and join them together. The book's Figure 4.1 shows the same program — adding the numbers 0 to 9 into a sum — in both styles. The Java (textual) version, as printed:
int sum=0;
for (i=0;i<10;i++) {
sum = sum + i;
}
The Scratch (visual) version of the same program is built from blocks:
set [sum] to (0)
repeat (10)
change [sum] by (i)
change [i] by (1)
Both versions do exactly the same work: start i and sum at 0, then ten times add i to the sum and increase i by 1.
4.2.1 Difference Between Visual and Textual Programming. The book's comparison table compares the two styles on four points:
| Point | Visual programming | Textual programming |
|---|---|---|
| Representation | Picture blocks that are dragged and joined | Typed code in a written language |
| Ease | Easier to start; blocks fit only where they make sense | Needs exact typing and syntax |
| Audience | Beginners and students | Programmers writing larger programs |
| Examples | Scratch | Java |
📚 Book Vocabulary — Words in this part
visual programming — Making programs by dragging and joining picture blocks instead of typing code. The book contrasts it with textual programming (§4.2, printed p 93). ⭐Book word
textual programming — Making programs by typing code in a written language, such as the Java code shown beside the Scratch blocks in Figure 4.1 (§4.2, printed p 93). ⭐Book word
🌱 Extra Help Words — Words in this part
block palette — The list of block categories on the left of the Scratch editor — Motion, Looks, Sound, Events, Control, Sensing, Operators, Variables and My Blocks.
Part 3 — The building blocks, and 4.3 Sequential Statements (printed p 94)
Before §4.3, the book introduces the three building blocks from which Scratch programs are made: Sequence (blocks in order), Repetitive (loops that repeat blocks) and Conditional (blocks that run only when a condition is true). This chapter studies each of them in turn.
4.3 Sequential Statements. In a sequence, the blocks run one after another, in the order they are stacked. The book's worked example:
move (10) steps
say (Hello!) for (2) seconds
How it works: (1) the program starts when the green flag is clicked; (2) the sprite moves 10 steps forward; (3) the sprite says "Hello!" for 2 seconds. Prose and blocks agree exactly in this example.
📚 Book Vocabulary — Words in this part
sequence — One of the three basic building blocks of programs: blocks run one after another, in order, from top to bottom (§4.3, printed p 94). ⭐Book word
🌱 Extra Help Words — Words in this part
event — Something that starts a script, such as clicking the green flag or pressing a key.
Part 4 — 4.4 Conditional Statements and 4.4.1 if-then (printed pp 95–96)
4.4 Conditional Statements. A conditional statement runs blocks only when a condition is true. The book's "Remember" picture shows the Control palette blocks this chapter uses: wait 1 seconds, repeat 10, forever, if-then and if-then-else.
4.4.1 if-then. The book's example moves the sprite Giga with the arrow keys. The blocks of Figure 4.2(a), exactly as printed:
if <key (right arrow) pressed?> then
move (100) steps
if <key (left arrow) pressed?> then
move (-100) steps
The flow diagrams of Figure 4.2(b) say the same thing in picture form: Start → decision "Right Arrow Key Press" → True → "Move 100 Steps forward" → End; and Start → "Left Arrow Key Press" → True → "Move 100 Steps backward" → End.
📚 Book Vocabulary — Words in this part
sprite — A character or object on the Scratch stage that carries scripts and can move, speak and change look — for example Giga in this chapter. ⭐Book word
conditional statement — A statement that runs its blocks only when a condition is true; the book builds programs from sequence, repetitive and conditional building blocks (§4.4, printed p 95). ⭐Book word
if-then — A Control block that runs the blocks inside it only if its condition is true; if the condition is false, nothing inside runs (§4.4.1, printed p 96). ⭐Book word
Part 5 — Making a maze sprite stop at walls: Figures 4.2(c) and 4.2(d) (printed pp 97–98)
The book now develops the if-then idea towards a maze game. Figure 4.2(c) moves a sprite in a maze with a key-press script:
change x by (5)
Figure 4.2(d) adds wall collision. After moving, the sprite checks whether it is touching the wall colour (blue); if it is, it moves the same distance back:
change x by (5)
if <touching color (blue)?> then
change x by (-5)
Trace: press the right-arrow key away from any wall → the sprite moves +5 and the touching test is false, so it stays 5 steps further right. Press it against a blue wall → the sprite moves +5, the test becomes true, and it moves −5: net movement 0, so it looks as if the wall has stopped it. This bounce-back trick returns in both maze games (Parts 14–15).
Part 6 — 4.4.2 if-then-else: the Even–Odd Number Program (printed pp 98–100)
An if-then-else block has two branches: one for true, one for false, and exactly one of them always runs. The book builds a complete program that asks for a number and reports whether it is even or odd, in five steps:
- Step 1. Take input with the ask block (Sensing) and store it in a new variable named Number (Variables → Make a Variable).
- Step 2. The starting script:
set [Number] to (0)
ask (What's your number?) and wait
set [Number] to (answer)
- Step 3. Use the mod operator (Operators) to check whether the number divided by 2 leaves a remainder of 0.
- Step 4. The completed decision block:
say (The number is Even) for (2) seconds
else
say (The number is Odd) for (2) seconds
- Step 5. Steps 2 and 4 together are the finished program. Traced sample runs (as printed): input 15 → 15 mod 2 = 1, so the else branch says "The number is Odd"; the book's other pictured run gives "The number is Even".
📚 Book Vocabulary — Words in this part
if-then-else — A Control block with two branches: the first runs if the condition is true, the second (else) runs if it is false (§4.4.2, printed pp 98–100). ⭐Book word
mod operator — The Operators block that gives the remainder when one number is divided by another; (Number) mod (2) tests whether a number is even (§4.4.2, printed p 99). ⭐Book word
🌱 Extra Help Words — Words in this part
variable — A named store for a value that can change while the program runs, such as Number, score or counter.
remainder — What is left over after a division: 15 divided by 2 leaves remainder 1, so 15 mod 2 = 1.
Part 7 — 4.4.3 if versus if-else, and 4.4.4 Nested if-then-else (printed pp 100–101)
4.4.3 Difference Between if and if else condition. The book's table compares them on three points:
| Point | if-then | if-then-else |
|---|---|---|
| Condition outcomes | Only the true outcome has blocks | Both outcomes have blocks |
| Action for false | Nothing runs; the program moves on | The else branch runs |
| Usage | When an action is needed only if the condition is true | When a different action is needed when the condition is false |
4.4.4 Nested if-then-else statement. When an if is placed inside another if, the conditions are nested: the inner condition is checked only after the outer one. The book's scenario (prose, printed p 101) is a score message:
- If the score is 80 or more, say "Great job!"
- Else, if the score is between 50 and 79, say "Good effort!"
- Else, say "You need more practice!"
The blocks printed in Figure 4.3 are:
say (Great job!)
else
if <(score) > (50) or (score) < (79)> then
say (Good effort!)
else
say (You need more practice!)
| Score | Printed blocks say | The book's scenario intends |
|---|---|---|
| 49 | "Good effort!" ✗ | "You need more practice!" |
| 50 | "Good effort!" ✓ (but only because of the OR) | "Good effort!" |
| 79 | "Good effort!" ✓ | "Good effort!" |
| 80 | "Good effort!" ✗ | "Great job!" |
The corrected blocks (whole-number scores, so "> 79" means 80 or more, and "> 49" means 50 or more):
say (Great job!)
else
if <(score) > (49)> then
say (Good effort!)
else
say (You need more practice!)
Trace of the corrected blocks: 49 → not > 79, not > 49 → "You need more practice!" ✓ · 50 → "Good effort!" ✓ · 79 → "Good effort!" ✓ · 80 → "Great job!" ✓. All four boundaries now match the book's stated bands.
📚 Book Vocabulary — Words in this part
nested if-then-else — An if-then or if-then-else placed inside another if, so a second condition is checked only after the first one (§4.4.4, printed p 101). ⭐Book word
🌱 Extra Help Words — Words in this part
branch — One of the paths a conditional block can take — the 'then' path or the 'else' path.
Part 8 — Activity: is the number positive or negative — and even or odd? (printed pp 101–104)
The Activity box on printed p 101 sets a harder task: identify whether a given number is positive or negative, and (for the numbers it can) whether it is even or odd. The book builds it in six steps, nesting the Even–Odd block of Part 6 inside a new outer condition:
- Steps 1 and 3. The same start as the Even–Odd program: ask for a number and store it in the variable Number.
- Steps 2 and 4. The first question — positive or negative — is the outer condition. The even/odd check can only be made if the outer condition is true. The skeleton:
if <(Number) mod (2) = (0)> then
say (The number is Even) for (2) seconds
else
say (The number is Odd) for (2) seconds
else
say (The number is negative or zero.) for (2) seconds
- Step 5. The outer else branch (above) covers everything that is not greater than 0 with the single message "The number is negative or zero."
- Step 6. Run and verify. Printed sample runs: input 15 → "The number is Odd"; input −2 → "The number is negative or zero."
🌱 Extra Help Words — Words in this part
trace (dry run) — Following a program's blocks by hand, writing down the values step by step, to check what it will do before running it.
Part 9 — 4.5 Loops and 4.5.1 Types of Loops (printed pp 104–105)
4.5 Loop (Repetitive Statements). A loop repeats a set of blocks. In Figure 4.4 a forever loop makes a sprite meow again and again:
forever
wait (1) seconds
say (Meow)
start sound (Meow)
4.5.1 Types of Loops. Scratch has three loop blocks, and the book shows the same walking program built with each of them:
(a) repeat — runs its blocks a fixed number of times (Figure 4.5(a)):
point in direction (90)
repeat (10)
next costume
move (10) steps
wait (1) seconds
turn clockwise (15) degrees
(b) repeat until — repeats until its condition becomes true; here a counter variable counts the passes (Figure 4.5(b)):
point in direction (90)
set [counter] to (0)
repeat until <(counter) = (10)>
change [counter] by (1)
next costume
move (10) steps
wait (1) seconds
turn clockwise (15) degrees
(c) forever — never stops by itself; Figure 4.5(c) guards the actions with an if so they happen only while the counter is below 10:
point in direction (90)
set [counter] to (0)
forever
if <(counter) < (10)> then
change [counter] by (1)
next costume
move (10) steps
wait (1) seconds
turn clockwise (15) degrees
📚 Book Vocabulary — Words in this part
loop — A structure that repeats a set of blocks; the book's three loop blocks are repeat, repeat until and forever (§4.5, printed p 104). ⭐Book word
repeat — A loop block that runs its blocks a fixed number of times, e.g. repeat (10) (§4.5.1, printed p 105). ⭐Book word
repeat until — A loop block that repeats its blocks until its condition becomes true, and then stops (§4.5.1, printed p 105). ⭐Book word
forever — A loop block that repeats its blocks without end, until the project is stopped (§4.5.1, printed p 105). ⭐Book word
🌱 Extra Help Words — Words in this part
iteration — One complete run through the blocks inside a loop; a repeat (10) loop has 10 iterations.
Part 10 — 4.6 Nested Loops: Giga walks through the backdrops (printed pp 106–107)
A nested loop is a loop inside a loop. The book's program makes the sprite Giga walk across at least three backdrops: choose the Giga sprite and the backdrops, send Giga to the start, then run a repeat until loop (counting to 4) inside an outer repeat (10) loop; after each inner loop finishes, the backdrop switches. The complete code (printed p 107):
go to x: (-145) y: (-77)
repeat (10)
set [counter] to (0)
repeat until <(counter) = (4)>
change [counter] by (1)
next costume
move (10) steps
wait (1) seconds
wait (0.1) seconds
switch backdrop to (next backdrop)
Trace: the counter is reset to 0 at the start of every outer round, so the inner loop always runs exactly 4 times — Giga changes costume and moves 10 steps each time (40 steps per round) — and then the backdrop switches once. Over the 10 outer rounds the backdrop switches 10 times. The book's output pictures show Giga on three backdrops: a school building, a desert and a beach.
📚 Book Vocabulary — Words in this part
nested loop — A loop placed inside another loop; in §4.6 a repeat until loop runs inside a repeat (10) loop (printed pp 106–107). ⭐Book word
counter — A variable used to count how many times a loop has run; the loop programs of §4.5.1 and §4.6 use a counter variable. ⭐Book word
backdrop — The background picture of the stage; a program can switch backdrops, as the Giga walking program does after every outer loop round (§4.6, printed pp 106–107). ⭐Book word
Part 11 — 4.7 Functions and 4.7.1 Built-in Functions in Scratch (printed pp 107–109)
4.7 Functions in Scratch. A function is a ready-made or self-made piece of code that does one job. Scratch functions come in two kinds: built-in functions, which Scratch already provides, and custom functions, which you define yourself (Part 12).
4.7.1 Built-in functions. The book presents these genuine Scratch operators:
- pick random — chooses a whole number at random inside a range; the book's figure builds set counter to (pick random (1) to (10)).
- abs — the absolute value of a number, its distance from zero: abs of −9 is 9, and the book's figure shows (abs) of (−3) giving 3.
- join — joins two pieces of text (or text and numbers) into one: join (apple) (banana) gives "applebanana". It lives in the Operators category.
Rounding. The ( ) of ( ) operator block also rounds numbers. The book's table (printed p 109):
| Function | What it does | Printed example |
|---|---|---|
| round | Rounds to the nearest whole number | round (45.5) → 46 |
| floor | Always rounds a number down | (floor) of (45.8) → 45 |
| ceiling | Always rounds a number up | (ceiling) of (45.2) → 46 |
All three printed examples are correct. The book describes round as "rounds a number up if the decimal is greater than 4" — the precise rule is: look at the fractional part; if it is 0.5 or more, round up, otherwise round down (that is why 45.5 → 46).
The book then prints a table of "additional functions available in Scratch": sqrt( ), type( ), pow( ), max( ), bool( ), format( ) and help( ).
📚 Book Vocabulary — Words in this part
function — A named piece of code that performs a task and can be reused; the book divides Scratch functions into built-in functions and custom functions (§4.7, printed p 107). ⭐Book word
built-in function — A function Scratch already provides, ready to use — for example pick random, abs and join (§4.7.1, printed pp 108–109). ⭐Book word
absolute value (abs) — The distance of a number from zero, given by the (abs) operator: abs of −3 is 3, and abs of 0 is 0 (§4.7.1, printed p 108). ⭐Book word
round — An operator in the ( ) of ( ) block that rounds a number to the nearest whole number; round of 45.5 is 46 (§4.7.1, printed p 109). ⭐Book word
floor — An operator that always rounds a number down to the whole number below it; floor of 45.8 is 45 (§4.7.1, printed p 109). ⭐Book word
ceiling — An operator that always rounds a number up to the whole number above it; ceiling of 45.2 is 46 (§4.7.1, printed p 109). ⭐Book word
Part 12 — 4.7.2 Custom Functions: the Calculate Area block (printed p 110)
In the My Blocks palette, "Make a Block" lets you define your own block (Figure 4.6 shows the dialog). The book defines a block named Calculate Area with two inputs, length and width. Its definition (Figure 4.7):
set [area] to ((length) * (width))
The main script asks for the two inputs and then calls the block:
ask (length) and wait
set [length] to (answer)
ask (width) and wait
set [width] to (answer)
Calculate Area (length) (width)
say (area)
Worked trace: if the user enters length 4 and width 2, the block sets area to 4 × 2 = 8 and the sprite says 8 — the output pictured in the book (the book does not print which inputs produced its pictured 8, but 4 × 2 shows the working). Once defined, the same block can be reused for any rectangle without rebuilding it.
📚 Book Vocabulary — Words in this part
custom block (My Blocks) — A block you define yourself with Make a Block in the My Blocks palette, for example the Calculate Area block with inputs length and width (§4.7.2, printed p 110). ⭐Book word
🌱 Extra Help Words — Words in this part
argument — A value passed into a custom block when it is called, such as the 4 and 2 passed as length and width to Calculate Area.
Part 13 — 4.8 Cloning a Sprite (printed p 111)
Cloning makes copies of a sprite while the program runs. The book's example fills the sky with balloons. Two scripts work together (Figure 4.8(a)). The original sprite's script hides itself and keeps creating clones at random positions:
hide
forever
go to x: (pick random (-250) to (250)) y: (-200)
next costume
create clone of (myself)
wait (0.5) seconds
Each clone runs its own script when it starts: it shows itself, floats upward while shrinking, and then deletes itself:
show
repeat (50)
point in direction (pick random (80) to (100))
change y by (10)
change size by (-2)
wait (2) seconds
delete this clone
Figure 4.8(b) shows the result: many balloons floating over a field backdrop. Because every clone deletes itself at the end of its script, finished balloons disappear instead of piling up forever.
📚 Book Vocabulary — Words in this part
cloning — Making copies of a sprite while the program runs; each copy (clone) runs the script under 'when I start as a clone' (§4.8, printed p 111). ⭐Book word
Part 14 — 4.9.1 The Simple Maze Game (printed pp 112–113)
4.9 Game Design with Scratch puts everything together. In the Simple Maze Game the player guides a sprite through a maze of blue walls (Figure 4.9: blue walls, a red start dot at the top-left, and a small green goal square at the bottom-right) without touching the walls. The book's six steps:
- Step 1 — Create the maze: draw or choose a backdrop whose walls form the path.
- Step 2 — Add a sprite: the player, for example a ball or a character.
- Step 3 — Set up movement (on the player sprite):
go to x: (-205) y: (147)
when (up arrow) key pressed
point in direction (0)
move (10) steps
when (down arrow) key pressed
point in direction (180)
move (10) steps
when (right arrow) key pressed
point in direction (90)
move (10) steps
when (left arrow) key pressed
point in direction (-90)
move (10) steps
- Step 4 — Add wall collision (same sprite): a forever loop watches for the wall colour and pushes the sprite back the same distance it just moved:
forever
if <touching color (blue)?> then
move (-10) steps
- Step 5 — Set the goal: add a second sprite (for example a star) as the goal, and put this script on the goal sprite:
forever
if <touching (Ball)?> then
say (You win!)
- Step 6 — Execute your game: run it and check that the sprite moves correctly and the goal works.
The simple maze in schematic form (100MCQs drawing, from the book's Figure 4.9 description): blue walls, red start at the top-left, green goal at the bottom-right.
📚 Book Vocabulary — Words in this part
collision — When a sprite touches a wall or another sprite; the maze games detect it with touching color / touching blocks and make the sprite bounce back (§4.9.1, printed p 113). ⭐Book word
🌱 Extra Help Words — Words in this part
coordinates (x, y) — Two numbers that give an exact position on the stage: x for left–right, y for up–down.
Part 15 — 4.9.2 The Advance Maze Game: many levels (printed pp 114–119)
The book's second project (its heading spells it "Advance Maze Game") is a multi-level maze. The project files are obtained from a download link printed in the book (inventwithscratch.com) — your teacher can fetch them for the class. Three sprites work together: the Cat (the player, "Sprite1"), the maze itself as a sprite, and an Apple marking the exit at the top of the maze; the Cat enters at the bottom.
Step 2 — the Cat's skeleton script. The Cat's code is a forever loop holding five if blocks: four check the arrow keys, and the fifth checks the exit Apple. The skeleton (printed p 115):
set size to (10) %
go to x: (10) y: (-170)
erase all
forever
if <> then ①
if <> then ②
if <> then ③
if <> then ④
if <> then ⑤
The green flag starts the game, the Cat shrinks to 10% and goes to the starting point x: 10, y: −170, and erase all (a Pen block) clears any pen drawings. Each numbered if is now filled in:
Block ① — up arrow (printed p 116):
change y by (4)
next costume
if <touching (maze)?> then
change y by (-4)
else
change y by (-4)
change y by (4)
Block ② — down arrow (printed p 116):
change y by (-4)
next costume
if <touching (maze)?> then
change y by (4)
else
change y by (4)
change y by (-4)
Block ③ — right arrow (printed p 117):
change x by (4)
next costume
if <touching (maze)?> then
change x by (-4)
else
change x by (-4)
change x by (4)
Block ④ — left arrow (printed p 117):
change x by (-4)
next costume
if <touching (maze)?> then
change x by (4)
else
change x by (4)
change x by (-4)
How each movement block behaves (traced): the Cat first moves 4 points. If it is then touching the maze, it moves 4 points back — net movement 0, the bounce-back. If it is not touching, the else branch moves −4 then +4 (net 0 extra), so the original 4-point move stands. The else pair is redundant but harmless.
Block ⑤ — reaching the Apple (printed p 118):
say (You Win!) for (1) seconds
go to x: (10) y: (-170)
broadcast (next maze)
erase all
The broadcast block (Events palette) sends the message "next maze" to the maze sprite, whose receiver script changes the maze — the book says this "adds up the level and difficulty in the game":
next costume
Step 3 — the maze sprite's own code (printed p 119). At the green flag it picks a maze costume at random (the side panel says there are 100 maze backgrounds for levels and difficulty) and centres itself:
switch costume to (pick random (1) to (100))
go to x: (0) y: (0)
when I receive (next maze)
next costume
Step 6 — the Apple sprite's code:
set size to (25) %
go to x: (-10) y: (170)
Steps 7–8 — execute the game: with all the code in place, press the green flag and play. Two design facts to notice while you play: the Cat restarts at the same point (10, −170) on every level, and the Apple stays at the same place (−10, 170) on every level even though the maze picture changes; and the maze is a sprite carrying scripts (that is why touching (maze)? works), although the prose sometimes calls it a background.
📚 Book Vocabulary — Words in this part
broadcast — An Events message sent by one script and received by others; in the Advance Maze Game the Cat broadcasts (next maze) to change the level (§4.9.2, printed p 118). ⭐Book word
costume — One of the looks of a sprite; 'next costume' changes the look. The maze sprite changes costume to bring in a new maze (§4.9.2, printed pp 118–119). ⭐Book word
🌱 Extra Help Words — Words in this part
stage — The area of the Scratch window where the sprites perform and the game is seen.
level — One stage of a game; finishing it takes the player to the next, usually harder, stage.
Part 16 — Summary of the chapter (printed p 120)
The book's summary collects the chapter in nine points:
- Sequence — blocks execute one after another in order.
- Conditional statement — runs blocks only when a condition holds.
- if statement — one branch, taken when the condition is true.
- if-else statement — two branches, one for true and one for false.
- Loop — repeats a set of blocks.
- repeat loop — repeats 'x' times.
- repeat until loop — repeats until its condition becomes true.
- forever loop — repeats without end.
- Function — a named, reusable piece of code that performs a task.
That completes the chapter. Its Exercise is short — only 8 questions — and every one is solved in the next tab. One honest note: no Scratch editor was available while this page was written, so every program above has been verified by careful hand-tracing (inputs → steps → outputs), and running the projects in Scratch remains a classroom step.
