Introduction
A loop repeats a set of instructions until a stop rule is met. Instead of writing “ring bell” five separate times, you write one ringing action and loop it five times—or until a sensor says stop. Loops save effort, reduce copy-paste mistakes, and power animations, quizzes, and data processing.
This lesson builds on algorithms, variables, and logic. You will meet counted loops and conditional loops, practice tracing, and learn how stop conditions protect you. Later, functions and conditions will sit inside and around loops in richer programs. Keep typing fluent with practice so writing repeated structures feels light.
Learning Objectives
By the end of this lesson, you will be able to:
- Explain why loops beat duplicated steps
- Describe for-style and while-style repetition
- Trace changing loop variables on paper
- Write a clear stop condition
- Spot common loop mistakes beginners make
Main Lesson
Life without loops (painful)
Imagine printing seats 1 to 20:
```text
PRINT "Seat 1"
PRINT "Seat 2"
...
PRINT "Seat 20"
```
Twenty lines, easy to mistype, painful to change to 200. A loop says: start at 1, print seat number, increase, stop after 20.
Two big loop families
| Loop style | Core idea | Typical use |
|---|---|---|
| Counted (for / repeat n times) | Repeat a known number of times | 10 quiz questions, 5 sparkles |
| Conditional (while / repeat until) | Repeat while a condition stays true | Keep asking until password correct |
| For each (collection loop) | Repeat once per item in a list | Email every student in a roster |
Different languages spell these differently. In block coding, you often see “repeat,” “forever,” and “repeat until.”
Counted loop anatomy
Pseudocode:
```text
FOR i FROM 1 TO 5 DO
PRINT "Hello"
END FOR
```
iis a loop variable (counter)- Body runs for each value of
i - When the range ends, the loop stops
Trace table:
| Pass | i | Output moment |
|---|---|---|
| 1 | 1 | Hello |
| 2 | 2 | Hello |
| 3 | 3 | Hello |
| 4 | 4 | Hello |
| 5 | 5 | Hello |
While loop anatomy
```text
SET lives TO 3
WHILE lives > 0 DO
PLAY round
IF player lost THEN lives ← lives - 1
END WHILE
PRINT "Game over"
```
The condition lives > 0 is checked based on language rules (typically before each pass). If nothing ever reduces lives, the loop may never end—an infinite loop.
Infinite loops: causes and cures
Common causes:
- Forgot to update the variable used in the condition
- Condition always true (
WHILE truewithout a break plan) - Updating the wrong variable
- Logic that accidentally resets the stop flag
Cures:
- Define the stop condition first
- Ensure each loop pass moves closer to stopping
- Cap retries (
attempts < 3) - In tools with a stop button, know how to halt runaway loops
Loops + decisions
Loops and conditions team up:
```text
FOR each answer IN answers DO
IF answer is correct THEN score ← score + 1
END FOR
```
This pattern grades a whole quiz list without hand-writing each check.
Loops in real apps
| Feature | Loop idea |
|---|---|
| Slideshow autoplay | Repeat advance until last slide or forever with pause |
| Notification list | For each alert, draw a row |
| Animation frames | Repeat draw/update while game runs |
| Typing analysis | For each character typed, compare to target—as on TYPE10X Practice |
Whenever you hear “for every…” or “keep doing until…,” you are hearing a loop.
Nesting (gentle preview)
A loop inside a loop can build grids (rows and columns). Beginners should master one loop first; nesting is easier after tracing feels natural.
Key Definitions
- Loop — A control structure that repeats instructions.
- Iteration / pass — One run of the loop body.
- Loop body — The instructions repeated.
- Loop variable / counter — A value tracking progress through counted loops.
- While loop — Repeats while a condition remains true.
- For loop — Often repeats across a range or collection.
- Infinite loop — A loop that never meets its stop condition.
- Break (intro) — An early exit from a loop in some languages.
- Continue (intro) — Skip the rest of the current pass in some languages.
- Sentinel value — A special input that means “stop looping” (like typing
quit).
Examples
Example 1: Sum numbers 1 to 5
total ← 0 → for i from 1 to 5: total ← total + i → total becomes 15.
Example 2: Password retries
While attempts < 3 and not success: ask password; if wrong, attempts ← attempts + 1.
Example 3: Countdown
For n from 10 down to 1: display n; then display “Go!”
Example 4: Average score
For each score in list: add to total; then divide by count.
Real-World Scenarios
Scenario A — Fundraiser tickets
Students print 100 identical labels using a repeat block instead of duplicating stacks. Changing the logo once updates all iterations’ design steps.
Scenario B — Sensor robot
While distance > 10 cm, drive forward. When the condition fails, stop. The stop rule is as important as the move rule.
Scenario C — Frozen browser tab
A beginner wrote WHILE true that created endless alerts. Learning to stop the script and add a limit becomes a survival skill.
Tips
questionNumber) over mystery names (i) when learning—use short counters later when comfortable.Warnings
Did You Know
Common Mistakes
- Off-by-one errors (looping 0–9 when you needed 1–10)
- Forgetting to update the condition variable
- Putting the stop check in the wrong place
- Duplicating code instead of looping when repetition is obvious
- Assuming the loop runs zero times or infinite times without tracing
Interactive Exercise
Loop Doctor (15 minutes)
Diagnose and fix these plans:
SET x TO 1/WHILE x > 0/PRINT x/ (no change to x)- Print numbers 1–10 but the plan uses
FOR i FROM 1 TO 10and printsi+1accidentally - Ask for names until the user types
done, but the plan never reads input inside the loop
Rewrite each correctly and trace one of them fully.
Practice Questions
- What problem do loops solve?
- How does a while loop decide to continue?
- What is an infinite loop?
- Why do trace tables help?
- Give a real-life “keep doing until…” example.
Mini Challenge
Design a loop algorithm for a “flashcard trainer”:
- Start with a list of 8 terms
- For each term, show prompt, accept answer, update score
- After the list, show final score and accuracy percent
Present a 90-second demo using paper cards if needed.
Summary
Loops repeat instructions with counted ranges or logical stop conditions. They make programs shorter, clearer, and more powerful—but only when stop rules and updates are correct. With variables tracking progress and logic deciding continuation, loops become a core coding habit. Next you’ll package reusable actions as functions.
Student Checklist
- [ ] I can explain counted vs conditional loops
- [ ] I can trace a simple loop
- [ ] I can describe infinite loop risks
- [ ] I fixed at least one broken loop plan
- [ ] I completed Loop Doctor and the mini challenge
Teacher Notes
- Physically loop students around the room N times for counted vs while “until you reach the door.”
- Use a projected trace table and fill it live.
- Ban “forever” until students can explain an exit strategy.
- Provide buggy pseudocode as stations.
- Connect to upcoming functions: “What repeated chunk could become a named action?”
FAQ
Q: Is a loop always faster than writing steps by hand?
For humans, yes in maintenance. For computers, loops are the natural way to process large counts and lists.
Q: Which should I learn first, for or while?
Either. Many classrooms teach repeat-n-times first because stop rules feel obvious.
Q: Do block “forever” loops count?
Yes—they are loops with an external stop (stop sign / window close) rather than a counted end.
Q: What is an off-by-one error?
Starting or stopping one step too early or too late—very common with ranges.
Q: What should I learn next?
Continue to Functions to bundle reusable actions into named blocks of logic.
Related Lessons
Related Blog Posts
- Keep learning with articles on the TYPE10X Blog
- Improve input speed for coding notes via Practice
Next Lesson CTA
You can now repeat work safely with stop conditions. Next, learn how to package actions into reusable named tools—continue to Functions.