Back to Real Loops
Code Studio · Level 4

Loop Builder

Students change the counter, repeat limit, and output message in a real JavaScript loop.

20-25 min Ages 9-12 Builder JavaScript

Loop power-ups

You know the loop recipe: for (start; keep going; add 1) { repeat this }. This builder mixes loops with everything else you've learned — variable boxes, glue, and counters with better names.

  1. 1

    Name your counter anything

    i is just a popular nickname. This example counts with count — you could use step, lap, or starNumber. Pick a label that says what you're counting, and use the same name in all three parts of the recipe.

  2. 2

    Loops + variables + glue, all together

    say(action + " " + count) opens the action box, glues it to the counter, and prints "Collect star 1", "Collect star 2"... Change the value in the action box once, and every line of output changes.

  3. 3

    Experiment with the numbers

    Make it count to 8. Make it start at 3 — how many lines print now? Predict first, then run. (Starting at 3 and stopping at 4 gives just two laps: 3 and 4.)

Code Editor

Customize the repeat.

Example code
let action = "Collect star";

for (let count = 1; count <= 4; count++) {
  say(action + " " + count);
}
Output

Program result

Robot ready to speak your strings

Type real JavaScript, then run it.