← Back to the map
🧰 Builder's Toolkit

πŸ“š Lists & Loops

Lists & Loops illustration

Say you had 100 coins to collect. You would notwrite β€œcollect coin 1, collect coin 2, collect coin 3…” a hundred times β€” that's bananas. 🍌 There's a much smarter way.

Put all the coins in a list, then use a loop to run one rule on every single one.

1

The big idea: one rule, the whole list

A list is just a row of boxes that holds a bunch of things β€” all your coins, all your enemies, all the blocks in a level. Instead of a separate name for each, they live together in one list.

A loop is how you deal with the whole list at once: β€œfor eachthing in the list, do this.” You write the rule a single time, and the loop runs it on every item β€” 3 of them or 3 million. That's how one tiny bit of code can move a thousand stars or check a whole world.

2

Your turn: run a rule down a list

Watch the glowing cursor β€” that's the loop β€” walk the list and do the job to each coin. Make the list longer, switch the job, and notice you never have to write a second rule.

// the only rule you write β€” once
for each coin in the list:
collect it βœ“

One rule, written once β€” the loop runs it on every coin, however many there are.

πŸ“ How long is the list?

5 coins

πŸ”§ The job

⚑ Loop speed

done so far
0 / 5
3

Discovery missions

Try each one, tick it off, then peek at the secret.

0/5
  • πŸ‘€Run the loop with 5 coins and watch the glowing cursor. What does it do at each coin?

  • πŸ“Slide the list up to 12 coins and run it again. Did you have to add any new rules for the extra coins?

  • 🌈Switch the job to Paint and run. The job changed β€” but did the loop change?

  • 🀯Imagine the list had ONE THOUSAND coins. How much more code would you write?

  • πŸͺ™Remember collecting coins in your game. How do you think it checked all of them?

🌟 New thinking tool unlocked! You now know:

🐞Next in the toolkitFind the Broken Rule β†’Things break all the time β€” here's how to hunt down the one bad bit.
← Back to the map