Learning Goals 3 min
By the end of this lesson you will be able to:
- Build the four-block list-walk pattern: a counter variable, a repeat driven by length of, an action on item (i) of, and a change [i v] by (1).
- Use the pattern to read out a whole shopping list, item by item, without knowing in advance how long the list is.
- Modify the action inside the loop to do different jobs on each item — say it, score it, count it, compare it — and recognise that this same pattern appears in every list-using program ever.
Warm-Up — Cikgu Rahman's roll call 7 min
Cikgu Rahman wants Scratch to read out every name on his class register. The register is a list called students with 30 names. He writes this:
when flag clicked
say (item (1) of [students v]) for (1) seconds
say (item (2) of [students v]) for (1) seconds
say (item (3) of [students v]) for (1) seconds
say (item (4) of [students v]) for (1) seconds
He's only got 4 of 30 done. Predict — what's wrong with finishing this by typing 26 more say-blocks?
Reveal the answer
Three big problems. (1) It's slow and boring to type 30 blocks. (2) Next term a new student joins and the list becomes 31 — Cikgu has to add another say-block. Next term someone leaves — he has to delete one. The script never matches the list. (3) The L3 block cap is 20 blocks per stack — 30 say-blocks won't even fit.
The fix is the most important pattern in list programming: a loop that visits every item, no matter how long the list is. One counter variable, one repeat block, one item-reporter, one change. Today's whole lesson is that one pattern.
By the end of class, Cikgu's roll call will be five blocks total — and it'll work for 30 students, 300 students, or 3 students, with zero changes.
New Concept — the list-walk pattern 15 min
You already know all the ingredients from earlier lessons. Today we put them together into a pattern you'll use for the rest of your programming life.
The four ingredients
- A counter variable — usually called
i(short for "index"). Starts at 1, the number of the first slot. - A repeat () block, where the count is length of [list v] — so the loop runs once per item, automatically.
- Inside the loop: an action on item (i) of [list v] — say it, score it, whatever.
- Last block inside the loop: change [i v] by (1) — bumps the counter to the next slot for the next time around.
The pattern, written out
Here it is in its purest form, applied to Cikgu Rahman's roll call:
when flag clicked
set [i v] to (1)
repeat (length of [students v])
say (item (i) of [students v]) for (1) seconds
change [i v] by (1)
end
Walking through what happens
Say the register has 4 students: Aisha, Boon, Chen, Devi.
- Before the loop: i is set to 1. Length of students is 4, so the repeat runs 4 times.
- Round 1: i = 1. Cat says item (1) of [students v] → "Aisha". Then i changes to 2.
- Round 2: i = 2. Cat says item (2) of [students v] → "Boon". Then i changes to 3.
- Round 3: i = 3. Cat says "Chen". Then i changes to 4.
- Round 4: i = 4. Cat says "Devi". Then i changes to 5. Loop ends because we've already done 4 of 4 rounds.
Notice how the counter and the length work together — the repeat knows how many rounds to run, and the counter knows which slot to read this round.
Why "i" and not "counter" or "loopnumber"?
By tradition, the variable in this pattern is called i — it's short for "index" and every programmer since the 1950s has used it. You'll see i in Python, JavaScript, C, Java, and now Scratch. When you read other people's code and see a variable called i, it's almost always the counter in a loop-through pattern. Use the name i for the same reason — other readers (and future you) will spot the pattern instantly.
Why this pattern is everywhere
Change the action inside the loop and you change the program — but the loop shape stays the same:
- Say every item out loud → a roll call or shopping reader.
- Compare each item to the user's answer → a quiz auto-marker.
- Add each item's number to a total → summing a list of scores.
- Compare each item to a "best so far" variable → finding the biggest number in a list.
You'll meet all of these in the next few lessons. Today: just get the basic walk solid in your fingers.
Your first algorithm: linear search
An algorithm is just a step-by-step recipe a computer follows to get a job done. You've been writing little recipes all along — but this one has a name programmers have used for seventy years, and you're about to write it for real.
Here's the everyday version. Mak hands you her pasar list and asks, "Is telur on here?" You don't magically know — you run your finger down the list, line by line, checking each one: roti? no. telur? yes — stop, found it at line 2! If you reach the bottom without a match, you say "not on the list". That finger-down-the-list scan is linear search. It's the same thing you do queueing at the pasar looking for the auntie selling cili, scanning faces one by one until you spot her.
In Scratch we give the cat the same finger. A counter i is the finger; a found at variable remembers where we found it (or stays 0 for "not found"). We walk the list with a repeat until <> that stops either when we run off the end or the moment we get a hit:
when flag clicked
ask [Which item are you looking for?] and wait
set [found at v] to (0)
set [i v] to (1)
repeat until <<(i) > (length of [shopping v])> or <not <(found at) = (0)>>>
if <(item (i) of [shopping v]) = (answer)> then
set [found at v] to (i)
end
change [i v] by (1)
end
if <(found at) = (0)> then
say (join (answer) [ is not on the list]) for (2) seconds
else
say (join [Found at item ] (found at)) for (2) seconds
end
found at starts at 0 ("not yet found"); the loop fills it with the slot number the moment item (i) of [shopping v] matches the answer.Read it line by line — slowly, it's worth it:
- ask [Which item are you looking for?] and wait — Mak types the name she wants (it lands in (answer)).
- set [found at v] to (0) — start by assuming we haven't found it.
0is our code for "nowhere yet", because there is no item 0. - set [i v] to (1) — the finger starts on the first row.
- repeat until <<(i) > (length of [shopping v])> or <not <(found at) = (0)>>> — keep walking until one of two things is true: either
ihas gone past the last row (we ran out of list), orfound atis no longer 0 (we got a match). The or means "stop if either happens". - Inside: if <(item (i) of [shopping v]) = (answer)> then — does the row the finger is on match what Mak typed? If yes, set [found at v] to (i) remembers the slot number.
- change [i v] by (1) — move the finger to the next row. (Always the last block inside the loop.)
- After the loop: if found at is still
0, nothing matched — say "not on the list". Otherwise say which item number it was found at.
Notice the clever bit: the loop stops early the instant it finds a match — it doesn't keep scanning the rest of the list for nothing. If telur is item 2 of a 50-item list, the cat checks 2 rows, not 50. That "stop as soon as you know the answer" idea is one of the first things real computer scientists learn.
Worked Example — Hafiz's shopping reader 12 min
Hafiz wants Scratch to read out his mum's full shopping list, item by item, with a "Item N: X" message. We'll build it in seven steps using the pattern.
i walks down the shopping watcher; when row 2 (telur) matches Mak's search, the cat (the host sprite) reports "Found at item 2!". The plain list-walk you build first is the engine that powers this 🔍 Find.Step 1 — Start fresh
New project. Default cat. Make a list called shopping. Make a variable called i (uncheck its checkbox — we don't need it on the Stage). Tick the shopping list checkbox so you can see it grow.
Step 2 — Hat + reset
From Events: when ⚑ clicked. From Variables: delete all of [shopping v].
Step 3 — Add the shopping items
Four add blocks: nasi lemak, roti canai, milk, kuih lapis. (You can put anything — the loop won't care.)
Step 4 — Set the counter to 1
From Variables: set [i v] to (1). This is the most important block in the whole script. Forget it and the loop reads stale i-values from a previous run.
Step 5 — Drop the repeat with length inside
From Control: repeat (). From Variables, drag length of [shopping v] into the slot. Snap the whole thing under the set.
Step 6 — Say "Item i: item-i" inside the loop
Inside the C-block: say () for (1) seconds. In its slot, drop join () (). Left side: join [Item ] (i). Right side: join [: ] (item (i) of [shopping v]). You'll nest two join blocks — it gets fiddly but the result is a nice "Item 1: nasi lemak" message.
Step 7 — Bump the counter
Still inside the loop, after the say: change [i v] by (1). This is the block that makes the loop move forward.
The full assembled stack
when flag clicked
delete all of [shopping v]
add [nasi lemak] to [shopping v]
add [roti canai] to [shopping v]
add [milk] to [shopping v]
add [kuih lapis] to [shopping v]
set [i v] to (1)
repeat (length of [shopping v])
say (join (join [Item ] (i)) (join [: ] (item (i) of [shopping v]))) for (1) seconds
change [i v] by (1)
end
What you just built: the pattern that powers half of all real-world programs. Reading every row in a spreadsheet, every email in your inbox, every player in a game's leaderboard — all of them are some version of this exact five-block shape. You'll never stop using it.
Try It Yourself — three loop-through drills 15 min
Goal: Make a list called friends with four names. Use the list-walk pattern to make the cat say Hi, NAME! to every friend.
when flag clicked
delete all of [friends v]
add [Aiman] to [friends v]
add [Bavani] to [friends v]
add [Chen] to [friends v]
add [Devi] to [friends v]
set [i v] to (1)
repeat (length of [friends v])
say (join [Hi, ] (item (i) of [friends v])) for (1) seconds
change [i v] by (1)
end
Think: If you add a fifth friend, the loop automatically greets them too — without you touching the loop. That's the auto-adjust magic of length of inside the repeat.
Goal: Make a list called scores with five numbers: 80, 92, 75, 88, 90. Use the list-walk pattern to add them all up into a variable called total. Cat says the total at the end.
when flag clicked
delete all of [scores v]
add (80) to [scores v]
add (92) to [scores v]
add (75) to [scores v]
add (88) to [scores v]
add (90) to [scores v]
set [total v] to (0)
set [i v] to (1)
repeat (length of [scores v])
change [total v] by (item (i) of [scores v])
change [i v] by (1)
end
say (join [Total: ] (total)) for (2) seconds
Think: Two counters! total accumulates the sum, i walks the list. They have completely different jobs. Never mix them up — give them different names so future-you can read the script.
Goal: Make a list called quiz with three correct answers: red, blue, green. Use the list-walk pattern to ask one question per item, compare the user's answer to item (i) of [quiz v], and count how many they got right in a score variable.
when flag clicked
delete all of [quiz v]
add [red] to [quiz v]
add [blue] to [quiz v]
add [green] to [quiz v]
set [score v] to (0)
set [i v] to (1)
repeat (length of [quiz v])
ask (join [Question ] (i)) and wait
if <(answer) = (item (i) of [quiz v])> then
change [score v] by (1)
end
change [i v] by (1)
end
say (join [You got ] (score)) for (2) seconds
Think: You've just built a self-marking quiz. Change the list contents and the quiz automatically changes — no script edits needed. This is the start of data-driven programming, one of the biggest ideas in software.
Mini-Challenge — Devi's stuck loop 5 min
"The cat that says item 1 forever"
Devi wrote this stack to read out her favourite foods. She runs it — the cat says nasi lemak, then nasi lemak, then nasi lemak, never moving on. The list definitely has four items in it.
when flag clicked
set [i v] to (1)
repeat (length of [foods v])
say (item (i) of [foods v]) for (1) seconds
end
change [i v] by (1)
What did Devi do wrong, and how does she fix it?
Reveal one valid solution
The change [i v] by (1) is outside the repeat C-block, not inside. So i stays at 1 for all four loop rounds, and the cat reads slot 1 four times. After the loop ends, i finally changes to 2 — too late, the loop is over. The fix is to drag the change inside the C, as the last block:
when flag clicked
set [i v] to (1)
repeat (length of [foods v])
say (item (i) of [foods v]) for (1) seconds
change [i v] by (1)
end
Five blocks. One was on the wrong side of the C-block fence. The cat now correctly reads slot 1, then slot 2, then slot 3, then slot 4. The change-i block lives inside the loop, as the last block, always. Stick this in your memory — it's the #1 bug in list-walk code, even for grown-up programmers.
Recap 3 min
You met the most important pattern in list programming — the list-walk. Five blocks: clear-the-list (optional), set counter i to 1, repeat length of times, do something with item (i) of inside, then change [i v] by (1) as the last inside block. The shape never changes — only the action inside the loop changes between programs. With this pattern you can say every item, sum every number, mark every answer, find the biggest, find the smallest, count how many match. Every list-using program you'll ever write is some version of this shape. And built on top of it today, you wrote your first real algorithm — linear search: walk the list, compare each item, stop the moment you find a match.
- Counter variable
- A variable (almost always named
i) that keeps track of which slot the loop is currently on. Starts at 1; goes up by 1 each round. - Index
- The slot number of an item in a list. In Scratch, indexes start at 1 (not 0). The first item is at index 1, the last is at index length of [list v].
- List-walk
- The pattern set [i v] to (1) + repeat (length of [list v]) + action on item (i) of [list v] + change [i v] by (1). Visits every item, in order, regardless of list size.
- Data-driven
- A program whose behaviour is decided by the contents of a list (or other data), not by which blocks are wired up. Change the data, change the program — without touching the script.
- Accumulator
- A variable used inside a loop to gather a running result — like
totalfor summing orscorefor counting matches. Set to 0 before the loop, changed inside the loop, read after. - Algorithm
- A clear step-by-step recipe a computer follows to get a job done. Linear search is one; bubble sort (next lesson) is another. Both are built out of the list-walk pattern.
- Linear search
- The algorithm that finds an item by walking a list from the start, comparing each item to the target, and stopping at the first match (or reporting "not found" at the end). The hidden engine inside the <[list v] contains ()?> block.
Homework 2 min
The Three-Walk Drill. One Scratch project, one list called numbers with six numbers of your choice, three different list-walks. Each walk uses the same five-block shape but does a different job.
- when [s v] key pressed: sum all numbers into a
totalvariable. Cat says the total. - when [b v] key pressed: find the biggest number. Set a variable
bestto 0 before the loop. Inside the loop, if <(item (i) of [numbers v]) > (best)> then setbestto that item. Cat saysbest. - when [c v] key pressed: count how many numbers are bigger than 50. Set
countto 0, walk the list, incrementcountwhen the item passes the test.
Save as HW-L3-12-Three-Walks.sb3. Hit the flag, then press S, B, and C in turn — each should give the right answer for your list.
Bring back next class:
- The
.sb3file. - Your answer to: "All three handlers have the same five-block loop shape. Which blocks change between them, and which stay exactly the same?"
Heads up for next class: SCR-L03-13 uses everything from lessons 8-12 to build a full Shopping List Helper mini-project — add items, edit items, walk the list and read it out, ask questions about it. The list-walk pattern is the engine inside that whole project.