Loop
THE LOOP - *when a program must do the same thing over and over, you do not copy the instruction a hundred times; you write it ONCE inside a loop that says 'repeat this,' and the computer does every repetition for you.*
Loop the Beetle and the Long, Long Walk
Loop is a round, patient, hard-working beetle, and Loop has strong opinions about wasted effort - specifically, about writing the same instruction more than once.
Here is the thing that gets Loop going. Say the hero needs to walk twenty steps across a bridge. A brand-new coder, faced with this, will often write: step, step, step, step, step… twenty separate times, one for each step, lining them up in a great long tower of identical instructions. It works! The hero crosses the bridge. But Loop watches this and winces.
“Twenty times you wrote the same word,” Loop says, shaking its shiny head. “And if the bridge were a hundred steps? You’d write it a hundred times? What if you counted wrong and wrote nineteen? What a lot of work, and what a lot of ways to slip up.”
Then Loop shows the better way, and it is genuinely a little bit magic. Loop writes the instruction just once - step - and wraps it in a loop: “repeat this, 20 times.” That’s the whole program. One instruction, one number, and the loop does all twenty steps itself.
“See?” Loop says. “You didn’t do the repeating. I did the repeating. You just told me the step, and how many times.” The hero crosses the exact same bridge, but the code that sent it is short, clear, and almost impossible to miscount. Want a hundred steps instead? Change the 20 to a 100. Done. You didn’t write a single extra word.
That is the whole idea of a loop, and it is one of the most powerful ideas in all of coding. A loop is a way of saying “do this again and again” without writing “this” again and again. The computer is tireless and it never gets bored, so let it do the boring repeating. Your job is just to describe the one step, and how often.
There’s a catch Loop is careful to mention, though. A loop only helps when every repetition is really the same step. “If each step were different - go left, then jump, then wait - you couldn’t loop them all as one,” Loop says. “A loop repeats sameness. That’s its gift and its limit.” Spotting when something is truly repetitive is half the skill.
That is Loop’s tireless, tidy idea.
Write the repeated step ONCE, say ‘do it again and again,’ and let the loop do the repeating.
A grown-up can play too! Look around for real-life “loops” - things done the same way over and over. “Stir until it thickens.” “For every plate, wash, rinse, dry.” Notice how the instruction is short even though it happens many times. That’s exactly what a loop is: one description, many repetitions.
When the hero marches its twenty tidy steps across the bridge and Loop’s single wrapped instruction does all the counting, Loop gives a contented little click. “One step, written once, walked twenty times. That’s not laziness,” Loop says. “That’s smart. Why do a hundred times what you can say just once?”
Say it once, repeat it forever. That’s Loop.
The scriptquest ensemble
Loop is part of scriptquest's distributed-narrative cast. Each character embodies a different curricular primitive; together they teach the full subject.