Trek

LOOP / ITERATION — *keeps going around until the work is done.*

Loading audio…

Press play to listen along. The line being read lights up as you go.

Show full transcript

Loading transcript…

01 Opening
Trek beat 1 of 5

Trek wasn't a furry creature or a squishy blob. Trek was a small round track, painted bright yellow, with little arrows pointing all the way around to show the way to go, and a tiny screen on its front that showed numbers.

Trek's whole job was to loop — to do a thing again and again until it was done. It was the master of repetition, the champion of doing-it-over. Trek showed how a program runs the same block of steps over and over, which is called looping, or iteration.

02 Trek
Trek beat 2 of 5

One sunny morning Pip — a zippy little Variable, always changing, always moving — zoomed into the plaza holding a big empty bucket. "Trek! We need water balloons! Lots of them, for the big water fight!" Trek rolled to the Water Spout, held a blue balloon under it until — POP — it was full, tied it off, dropped it in the bucket, and its counter flashed 1. "Again!" said Pip. Trek filled another — 2 — and another — 3, 4, 5 — until Pip suddenly gasped, "Wait — how many do we need?" Trek paused, arrows glowing faintly. "Oh! Exactly ten!" Trek's eye brightened: a stopping condition. It went back to work — 6, 7, 8, 9, 10 — and when the counter hit ten, Trek stopped cold. The arrows faded. "Perfect!" Pip cheered. (If Pip had never said ten, Trek would've kept going, and going, and going, and buried CodeRealm in balloons forever — that's an infinite loop, and it's why you always need a stopping condition.)

03 Trek
Trek beat 3 of 5

"I could've filled them one by one," Pip mused, looking at the full bucket. Trek just blinked. Filling ten by hand takes a while — and what about a hundred? A thousand? Trek's way was better: it repeated the same steps automatically. No need to write out fill balloon, tie balloon, drop in bucket ten separate times — you just say do it ten times. That's the power of a loop: it saves you from endless copy-paste. And that little counter on Trek's screen — that was its counter variable, keeping track of how many times it had gone around, like a scoreboard for the work.

04 Trek
Trek beat 4 of 5

Just then a green balloon rolled by, and Pip gasped: "That one's got a hole!" Trek reached for it, spotted the leak, and nudged it aside with a wheel — skipping the bad one and moving straight to the next good balloon. No point filling a leaky balloon. A little later Pip called, "Now we need a red one — just one!" Trek filled balloons — blue, yellow, green, no red — then found a bright red one, filled it, set it in a special spot, and stopped. It didn't need any more. "Sometimes," Trek's arrows seemed to say, "you skip a step, and sometimes you stop early." Those two moves have names: continue (skip this one, keep looping) and break (stop the loop right now). They let you steer the flow instead of blindly repeating.

05 Closing
Trek beat 5 of 5

"New plan!" Pip announced. "Five buckets of ten balloons each!" Trek's eye widened — that was a loop inside a loop, a nested loop. It would loop ten times to fill one bucket, and loop five times to do that for five buckets, its screen now reading Bucket 1 of 5 · Balloon 1 of 10. Fill, tie, place — ten times — bucket one done, set aside. Then bucket two. It took a while, but soon five full buckets stood ready: fifty balloons. "Amazing!" Pip gasped. Trek just blinked — it was all still repeating, only with more layers. A slinky figure named Coil slithered past — Coil was a master of recursion, doing things by calling itself over and over, a twistier cousin of the loop. "Keep on looping, Trek!" Coil waved. Loops and recursion: cousins, both about doing a thing again until the job is done.

And Pip flopped down beside the fifty finished balloons, worn out but happy, a warm glow of pride spreading through them. The job that had felt huge and impossible at the start — fifty balloons, one at a time? never! — now felt light, and done. That easy, satisfied, big-repeating-job-finally-finished feeling — the deep exhale when the work is complete and you didn't have to grind through it by hand — that was the best part of the whole day. That's what a good loop gives you: it carries the repeating so you get to feel the finish.

The CodeRealm ensemble

Trek is part of CodeRealm's distributed-narrative cast. Each character embodies a different curricular primitive; together they teach the full subject.

Kids also liked