Computer Science · Coding Basics

Loops & Repeated Action

A loop tells the computer to repeat an action — so you write the code once instead of typing it over and over.
🤖Why Loops? (Walk 100 Steps)
😫The HARD way
# type it 100 times!
Step Forward
Step Forward
Step Forward
… 97 more lines …
Step Forward
✋ Your hand would cramp!
😎The LOOP way
# write it ONCE
repeat 100 times:
    Step Forward
✅ Smaller, cleaner, easy to read!
🔁Two Kinds of Loops
🔢FOR LOOP
runs a SET number of times 1 2 3 4 5 counter → STOP at 5 🛑
Runs a specific number of times. A counter ticks up each pass — when it hits the limit, the loop stops.
🏃 "Run 4 laps" — exactly 4 times
WHILE LOOP
dishes left? TRUE → keep going wash one, then re-check FALSE 🛑
Runs while a condition stays true. It re-checks every pass and stops the moment it's false.
🍽️ "Wash WHILE dishes are left"
⚙️How One Loop Spins
CHECK stop? RUN action 1 trip = 1 iteration done 🛑
🌀 Every loop spins the same way:
1Check the condition — is it time to stop yet?
2Run the action inside the loop (this is one iteration).
3Go back and check again — repeat until the stop condition is met.
∞ forever
⚠️ Watch out: Infinite Loops!
If you forget to give the loop a way to stop, it runs forever and can freeze your program. Always add a stop condition (or a break).
🔑Key Terms
🔁Loop Code that repeats the same instructions over and over.
🔂Iteration A single pass through the instructions inside a loop.
🔢For Loop A loop that runs a SPECIFIC number of times.
While Loop A loop that runs WHILE a condition stays true.
Efficiency Doing more with less code or effort.
♾️Infinite Loop A loop with no exit condition that runs forever — usually a bug.
Remember: a loop saves you from repeating yourself — but ALWAYS give it a way to stop, or you'll build an infinite loop that never ends!
✏️ Computer Science Anchor Chart · Loops & Repeated Action
From ClickClass — hundreds of free printables at clickclassedu.com/printables