What is a loop? In programming, a loop is a set of instructions that a computer repeats. You write the steps once, and the loop runs them again and again until its stop rule says to finish. Each pass through the steps is called an iteration. Loops are how a game keeps checking your keys and how a night light keeps checking whether the room is dark.
Every loop answers one question: when do I stop? A for loop stops after a set number of passes. A while loop stops when a condition turns false. A forever loop never stops by itself. A timed loop runs, rests, then runs again.
This guide explains each type with plain examples, a comparison table and short code in Arduino C++ and Python. Then you build one blinking light four ways on the MakerBuddy board, with no code: once, forever, on a timer and while a sensor says so.
What a loop is: the body, the stop rule and each pass
Oak National Academy's lesson on count-controlled iteration (opens in a new tab) defines iteration as repeating a sequence of instructions inside a program loop. Every loop has three parts.
- 1
The body.
These are the steps that repeat, such as "light on, wait, light off, wait".
- 2
The stop rule.
This is the test that decides whether to go round again. Programmers call it the loop condition.
- 3
The thing that changes.
A counter goes up, a sensor reading moves or time passes. If nothing changes, the stop rule gives the same answer every time, and the loop never ends.
Why not copy the steps out ten times? MDN's guide to looping code (opens in a new tab) answers with circles: drawing 100 circles or 10,000 takes the same code in a loop, and only one number changes. A loop is also easier to fix, because a mistake in the body only needs fixing once.
You already follow loops. "Do ten star jumps" has a count. "Stir until the sugar dissolves" has a condition. Your heartbeat never stops, and "water the plants every morning" runs on a timer. Loops are one of the five ideas in our guide to how no-code rules teach real logic.

Types of loops in programming, compared
Here are the main loops side by side, with the matching Scratch block and MakerBuddy loop mode.
| Loop type | Stops when | Arduino C++ example | Scratch block | MakerBuddy loop mode |
|---|---|---|---|---|
| For loop (count-controlled) | It has run a set number of times | for (int i = 0; i < 3; i++) | repeat (10) | None built in: write the steps out |
| While loop (condition-controlled, test first) | Its test is false at the start of a pass; may run zero times | while (light < 40) | repeat until, with the test turned round | Condition |
| Do...while loop (condition-controlled, test last) | Its test is false at the end of a pass; always runs once | do { ... } while (reading < 100); | No direct block | No direct match |
| Forever loop (infinite loop) | Never, until the power goes off or someone stops it | loop() or while (true) | forever | Forever |
| Timed loop (interval loop) | Never; it rests between runs | if (millis() - last >= gap) | forever, with a wait block | Timer |
| Run once (not a loop) | After one pass | Code in setup() | when green flag clicked, no loop | No Loop |
- Loop type
- For loop (count-controlled)
- Stops when
- It has run a set number of times
- Arduino C++ example
for (int i = 0; i < 3; i++)- Scratch block
- repeat (10)
- MakerBuddy loop mode
- None built in: write the steps out
- Loop type
- While loop (condition-controlled, test first)
- Stops when
- Its test is false at the start of a pass; may run zero times
- Arduino C++ example
while (light < 40)- Scratch block
- repeat until, with the test turned round
- MakerBuddy loop mode
- Condition
- Loop type
- Do...while loop (condition-controlled, test last)
- Stops when
- Its test is false at the end of a pass; always runs once
- Arduino C++ example
do { ... } while (reading < 100);- Scratch block
- No direct block
- MakerBuddy loop mode
- No direct match
- Loop type
- Forever loop (infinite loop)
- Stops when
- Never, until the power goes off or someone stops it
- Arduino C++ example
loop()orwhile (true)- Scratch block
- forever
- MakerBuddy loop mode
- Forever
- Loop type
- Timed loop (interval loop)
- Stops when
- Never; it rests between runs
- Arduino C++ example
if (millis() - last >= gap)- Scratch block
- forever, with a wait block
- MakerBuddy loop mode
- Timer
- Loop type
- Run once (not a loop)
- Stops when
- After one pass
- Arduino C++ example
- Code in
setup() - Scratch block
- when green flag clicked, no loop
- MakerBuddy loop mode
- No Loop
The code follows Arduino's reference pages for for (opens in a new tab), while (opens in a new tab) and do...while (opens in a new tab), and the blocks follow the Scratch Wiki pages for forever (opens in a new tab), repeat (opens in a new tab) and repeat until (opens in a new tab).
Oak's lesson sorts loops into two families: count-controlled loops run a set number of times, and condition-controlled loops run until a condition is met or is no longer met. A forever loop is a condition-controlled loop whose condition is always true. A timed loop is a forever loop that waits for the clock.
The for loop: repeat a set number of times
Arduino's reference page for the for loop (opens in a new tab) says it repeats a block of statements, usually with a counter that counts up and ends the loop. Its first line has three parts.
- Initialisation runs once, first.
int i = 0makes a counter callediand sets it to 0. - Condition is tested before every pass. While
i < 3is true, the body runs. - Increment runs after every pass.
i++adds 1 toi.
for (int i = 0; i < 3; i++) {
blinkOnce(); // runs when i is 0, 1 and 2
}Follow the counter one pass at a time. This is called a trace table.
| Pass | i before the test | Is i < 3? | What happens |
|---|---|---|---|
| 1 | 0 | Yes | Blink, then i becomes 1 |
| 2 | 1 | Yes | Blink, then i becomes 2 |
| 3 | 2 | Yes | Blink, then i becomes 3 |
| None | 3 | No | The loop ends after three blinks |
- Pass
- 1
- i before the test
- 0
- Is i < 3?
- Yes
- What happens
- Blink, then i becomes 1
- Pass
- 2
- i before the test
- 1
- Is i < 3?
- Yes
- What happens
- Blink, then i becomes 2
- Pass
- 3
- i before the test
- 2
- Is i < 3?
- Yes
- What happens
- Blink, then i becomes 3
- Pass
- None
- i before the test
- 3
- Is i < 3?
- No
- What happens
- The loop ends after three blinks
Change i < 3 to i <= 3 and you get four blinks. That slip has a name: an off-by-one error.
Python's tutorial on for statements (opens in a new tab) explains that its for loop goes through the items of a sequence in order, instead of counting with a step and a stop test as C does. range(3) makes the sequence 0, 1, 2. The tutorial notes that the end number is never part of it.
for i in range(3):
print("blink", i)This prints blink 0, blink 1 and blink 2. In Scratch, the repeat block does the same job.
The MakerBuddy rule engine has no "repeat 3 times" mode. To blink three times, you write the steps out: LED On, LED Off, three times over, in a No Loop rule. That is six steps. A rule holds at most 10 steps, according to how rules run, so this trick stops at five blinks. Hitting that wall is a good moment to meet the for loop in code.
While and do...while loops: repeat while a condition is true
Arduino's reference page for the while loop (opens in a new tab) says a while loop keeps going until the test in its brackets becomes false. Something must change what is being tested, or the loop never ends: your code, like a counter, or the outside world, like a sensor.
The test comes first. Python's reference for the while statement (opens in a new tab) says the test may be false the very first time, and then the body never runs. Here is a countdown:
countdown = 3
while countdown > 0:
print(countdown)
countdown = countdown - 1
print("Lift off")It prints 3, 2, 1 and "Lift off". Delete countdown = countdown - 1 and it prints 3 forever, because nothing changes the test.
A do...while loop moves the test to the end. Arduino's do...while reference (opens in a new tab) says the body therefore always runs at least once. That suits "read the knob, and keep reading until it is turned up":
int reading;
do {
reading = analogRead(34); // the knob on the MakerBuddy board
} while (reading < 100);Python's list of compound statements (opens in a new tab) has while and for but no do, so Python programmers write while True: and leave with break. Scratch's repeat until block is the mirror image of while: it stops when its test becomes true.
The Condition loop mode is a while loop you can watch
On the MakerBuddy board, the Condition loop mode turns a while loop into something you can see. You pick one of ten sensors, an operator such as Less than (<) and a threshold. While the test is true, the board repeats the rule's steps. When it turns false, the rule finishes the round it is on, then stops. Lesson 26 builds an automatic nightlight this way.
That matches a while loop in code, where the test is checked at the top of each pass, so a pass that has started runs to its end. The rules notes add that the test must stay true for 0.3 seconds before the rule starts. The board also reads its sensors once a second, so a Condition rule reacts in about a second.
A loop inside a loop, inside the board you hold
The board's firmware uses while loops to join Wi-Fi. According to the network notes, it makes up to 3 tries, and each try checks 20 times, half a second apart. That is a nested loop: the outer loop counts tries, and the inner loop runs while the board is not connected and has checked fewer than 20 times. At worst it checks 3 × 20 = 60 times, which takes about 30 seconds, then gives up. That limit stops a board with the wrong password from waiting forever.
Forever loops: infinite loops on purpose
Every Arduino sketch is built around a forever loop. Arduino's reference for loop() (opens in a new tab) says that after setup(), loop() runs again and again, so the program can respond. Scratch's forever block (opens in a new tab) shows the idea in its shape: it has no bump at the bottom, because nothing after it would ever run.
An infinite loop can be a bug or a choice. MDN's looping code guide (opens in a new tab) warns that if the counter never moves towards the end, the loop runs forever and the browser either stops it or crashes. That is the bug. A thermostat, though, should never finish. For it, the forever loop is the point.
The MakerBuddy Forever loop mode jumps back to step 1 the moment the last step's delay ends, with no gap. Lesson 23 uses it to build a beacon, and it teaches three more things.
- A running rule locks what it controls. While a Forever rule drives the blue light, the light's dashboard switch is locked, and its card says "This control is used by Rule Engine. Manual control is overridden." Timer and Condition rules lock their parts too. Press Stop or Delete on the rule's row to take control back.
- Two loops on one part fight. Two Forever rules, one switching the buzzer on and one switching it off, take turns, and the buzzer stutters. Programmers call this a race condition. The fix is one part, one rule.
- Different modes are ranked by priority. Lesson 21 gives the order: Condition beats Timer, Timer beats Forever, and Forever beats No Loop.
Timed loops: repeat on a schedule
A timed loop runs, rests and runs again, like "take a reading every 15 minutes".
In Arduino code, the easy way is delay(), but Arduino's Blink Without Delay tutorial (opens in a new tab) explains the cost: the program pauses during a delay, so it can miss a button press. The better way checks the clock on every pass of loop(). The clock is millis(), the milliseconds since the program started. Arduino's millis() reference (opens in a new tab) says it goes back to zero after about 50 days and warns that a small type such as int causes errors, so use unsigned long.
if (millis() - lastFinish >= GAP_MS) {
blinkOnce();
lastFinish = millis(); // count the gap from the end of the blink
}The MakerBuddy Timer loop mode does this for you. A Timer rule runs all its steps straight away, waits for the interval, then runs them again. You set the interval in Hours, Minutes and Seconds boxes, and the form starts at 5 minutes. The wait counts from the end of each run, as the rules notes explain.
Worked calculation: how often does a timed rule really run?
Lesson 25 builds a hazard light: LED On with a 1,000 ms delay, LED Off with a 100 ms delay, and an interval of 10 seconds.
- 1
One run of the steps takes 1,000 + 100 = 1,100 ms, which is 1.1 seconds.
- 2
The interval starts when the run ends, so one full cycle is 1.1 + 10 = 11.1 seconds.
- 3
In an hour, that is 3,600 ÷ 11.1 ≈ 324 flashes, not 360.
To flash every 10 seconds, the steps and the interval must add up to 10. The boxes take whole seconds, so you cannot type 8.9. Instead, set the LED Off delay to 0 ms. One run now takes 1.0 second, and an interval of 9 seconds makes each cycle 10 seconds.
The board has no clock that knows the time of day. A Timer rule means "every 2 hours from now", never "at 8 am". Our guide to timers, intervals and schedules goes further.

Worked example: one blinking light, four loop modes
Lesson 21 sums up every rule as a loop mode plus steps. The loop mode says when the steps run. The steps say what happens. Keep the steps the same, change only the loop mode, and you see four different loops.
You need the board, USB power and a laptop, iPhone, iPad or Android phone with the dashboard open; choosing your dashboard explains the options. The board needs 2.4 GHz Wi-Fi, and a phone hotspot works. Its local connection has no password, so use a Wi-Fi network you trust and never open the board to the internet. No board yet? Try a rule in your browser on the MakerBuddy page for students.
Step 1: build the Blink rule with No Loop
- 1
Open the Rule Engine tab and type
Blinkas the Rule Name. - 2
Choose the No Loop loop mode.
- 3
Add Step: LED On, Value 255, Delay (ms) 500.
- 4
Add Step: LED Off, Delay (ms) 500.
- 5
Predict what you will see, then click Save Rule.
The blue light blinks once and goes dark. A new rule that is not a Condition rule runs as soon as you save it. Press Start on its row to run it again; it also runs once each time the board switches on. Notice that a step's delay comes after its action: "LED On, 500" means "turn the light on, then wait half a second". Lesson 22 practises this.
Step 2: make it Forever
Press Edit on the Blink row, choose Forever and click Update Rule. The rule restarts at once and never stops. Half a second on plus half a second off is one second per pass, so you get 60 blinks a minute.
Try the LED switch on the Dashboard tab. It is locked. Press Stop on the Blink row. The blinking stops wherever it was, so the light may stay on. Switch it off by hand.
Step 3: make it a Timer
Press Edit, choose Timer and set Hours 0, Minutes 0 and Seconds 5. Check the Minutes box, because it starts at 5. Click Update Rule.
Predict the gap, then time it. One run takes 1 second and the rule then waits 5, so a new blink starts every 6 seconds. That is 10 blinks a minute.
Step 4: make it a Condition
Read the Light Level card on the Dashboard twice: once normally, and once with your hand over the light sensor. Pick a number halfway between, such as 40. Press Edit, choose Condition, and set Sensor to Light Level (LDR), Operator to Less than (<) and Threshold to your number. Click Update Rule.
Nothing happens while the room is bright. Cover the sensor, and after about a second the light starts blinking. Lift your hand. The rule finishes the blink it is on, then stops dark, because the last step is LED Off. That step is your clean-up.
| Loop mode | What you saw | When it stops | The same loop in code |
|---|---|---|---|
| No Loop | One blink | After one pass | Lines in setup() |
| Forever | A blink every second | Only when you press Stop or Delete | loop() or while (true) |
| Timer, 5 seconds | A blink every 6 seconds | Never by itself; it rests between runs | A millis() check in loop() |
| Condition, light < 40 | Blinks only while the sensor is covered | When the test turns false, after the current round | while (light < 40) |
- Loop mode
- No Loop
- What you saw
- One blink
- When it stops
- After one pass
- The same loop in code
- Lines in
setup()
- Loop mode
- Forever
- What you saw
- A blink every second
- When it stops
- Only when you press Stop or Delete
- The same loop in code
loop()orwhile (true)
- Loop mode
- Timer, 5 seconds
- What you saw
- A blink every 6 seconds
- When it stops
- Never by itself; it rests between runs
- The same loop in code
- A
millis()check inloop()
- Loop mode
- Condition, light < 40
- What you saw
- Blinks only while the sensor is covered
- When it stops
- When the test turns false, after the current round
- The same loop in code
while (light < 40)

The same loops in Arduino code
When you are ready, you can write these loops yourself. Our guide to programming an ESP32 with the Arduino IDE covers the setup. Choose the board "ESP32 Dev Module" with "esp32 by Espressif Systems" version 3.x, as the Arduino development notes describe.
// The MakerBuddy loop modes, written as Arduino code, on the blue LED.
// Board: "ESP32 Dev Module" with "esp32 by Espressif Systems" 3.x.
// Uploading this sketch replaces the MakerBuddy firmware until you upload it again.
const int LED_PIN = 12; // the built-in blue LED
const int BUTTON_PIN = 5; // the built-in push button: reads LOW while pressed
const unsigned long GAP_MS = 5000; // the timed loop's gap: 5 seconds
unsigned long lastFinish = 0; // when the last timed blink ended
// One blink: on for half a second, then off for half a second.
void blinkOnce() {
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
}
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
// 1. Run once, like No Loop: code in setup() runs one time.
Serial.println("Run once");
blinkOnce();
delay(1000);
// 2. Count-controlled: a for loop blinks exactly 3 times.
for (int i = 0; i < 3; i++) {
Serial.print("for loop, pass ");
Serial.println(i);
blinkOnce();
}
delay(1000);
lastFinish = millis();
}
// 3. Forever: loop() starts again as soon as it ends.
void loop() {
// 4. Condition-controlled: a while loop blinks while the button is held.
while (digitalRead(BUTTON_PIN) == LOW) {
Serial.println("while loop: button held");
blinkOnce();
}
// 5. Timed: blink, then wait 5 seconds counted from the end of the blink.
if (millis() - lastFinish >= GAP_MS) {
Serial.println("Timed blink");
blinkOnce();
lastFinish = millis();
}
}- 1
blinkOnce()is the body,written once and used by every loop.
- 2
Code in
setup()runs once,like a No Loop rule. The for loop inside it blinks three times.
- 3
loop()is the forever loop. - 4
The while loop
blinks while you hold the button. The pull-up keeps the pin HIGH until the button connects it to ground, so it reads LOW while pressed. While this loop runs, nothing else in
loop()runs, so the timed blink waits. - 5
The timed blink
counts its gap from the end of the blink, like a Timer rule. Open the Serial Monitor at 115200 baud to see which loop is running.
Common loop mistakes, with causes and fixes
The program freezes and stops responding
Cause: an infinite loop you did not mean, such as a while loop whose test never changes. The countdown without countdown = countdown - 1 is one.
Fix: make sure the body moves the test towards false. Add a safety limit, like the board's 20-check Wi-Fi loop, or leave early with break. Arduino's break reference (opens in a new tab) says it exits a for, while or do...while loop straight away.
The loop runs one time too many or too few
Cause: an off-by-one error, such as i <= 3 instead of i < 3, or forgetting that range(3) stops at 2. A while loop whose test is false at the start runs zero times.
Fix: write a trace table for the first and last pass. If the body must run at least once, use a do...while loop.
The Forever light looks on all the time
Cause: the LED Off step has a delay of 0 ms, so the light is off for no time before the rule jumps back to LED On.
Fix: give both steps a delay you can see. Lesson 23 suggests 100 ms or more.
The Timer rule runs later than you expected
Cause: the interval counts from the end of each run, so the step delays add to the gap. Or the Minutes box still says 5.
Fix: add the step delays to the interval, as in the worked calculation.
The light stays on after a Condition rule ends
Cause: the rule finished its round, but its last step left the light on. Stopping a rule never switches anything off.
Fix: end the steps with an off step, or add a second rule for the opposite case. Our guide to if-then statements with real sensors builds that second rule step by step.
Teaching loops: standards, lessons and a quick quiz
The CSTA K–12 Computer Science Standards (opens in a new tab) put loops at every level. In grades K–2 (ages 5–7), students develop programs with sequences and simple loops (1A-AP-10). In grades 3–5 (ages 8–11), they add events and conditionals (1B-AP-10). In grades 6–8 (ages 11–14), they use nested loops and compound conditionals (2-AP-12). In England, the national curriculum for computing (opens in a new tab) asks key stage 2 pupils to use sequence, selection and repetition in programs.
The free MakerBuddy curriculum, for ages 10 and up, teaches loops on the board across Terms 3 and 4.
| Lesson | Loop idea | What students build |
|---|---|---|
| Lesson 20 | Flowcharts with a loop back arrow | A plan that checks the light again and again |
| Lesson 21 | The four loop modes | A tour of the rule form |
| Lesson 22 | Run once | A welcome sequence |
| Lesson 23 | Forever loops and race conditions | A beacon, then a buzzer clash |
| Lesson 25 | Timed loops | A hazard light and a metronome |
| Lesson 26 | Condition loops | An automatic nightlight |
| Lesson 27 | Two tests joined with AND or OR | A threshold window |
- Lesson
- Lesson 20
- Loop idea
- Flowcharts with a loop back arrow
- What students build
- A plan that checks the light again and again
- Lesson
- Lesson 21
- Loop idea
- The four loop modes
- What students build
- A tour of the rule form
- Lesson
- Lesson 22
- Loop idea
- Run once
- What students build
- A welcome sequence
- Lesson
- Lesson 23
- Loop idea
- Forever loops and race conditions
- What students build
- A beacon, then a buzzer clash
- Lesson
- Lesson 25
- Loop idea
- Timed loops
- What students build
- A hazard light and a metronome
- Lesson
- Lesson 26
- Loop idea
- Condition loops
- What students build
- An automatic nightlight
- Lesson
- Lesson 27
- Loop idea
- Two tests joined with AND or OR
- What students build
- A threshold window
A good warm-up is Lesson 21's "Guess the loop mode" challenge. Students pick a mode for each job and explain why the other three are wrong.
| The job | Best loop mode | Why |
|---|---|---|
| A factory siren that sounds non-stop during an emergency | Forever | It repeats with no gap |
| A reminder that beeps every 20 minutes to drink water | Timer | It repeats on a schedule |
| A streetlight that turns on when it gets dark | Condition | It reacts to a sensor |
| A green "system online" light that comes on once at start-up | No Loop | It happens one time only |
- The job
- A factory siren that sounds non-stop during an emergency
- Best loop mode
- Forever
- Why
- It repeats with no gap
- The job
- A reminder that beeps every 20 minutes to drink water
- Best loop mode
- Timer
- Why
- It repeats on a schedule
- The job
- A streetlight that turns on when it gets dark
- Best loop mode
- Condition
- Why
- It reacts to a sensor
- The job
- A green "system online" light that comes on once at start-up
- Best loop mode
- No Loop
- Why
- It happens one time only
Our guide to flowcharts for beginners shows how to sketch each loop first. The lesson planner for teachers turns each lesson's three questions into a printable exit ticket.
FAQ
What is a loop in simple terms?
A loop is a set of steps that a computer repeats. You write the steps once, and the loop runs them again and again until a stop rule says to finish. Each pass is called an iteration.
What are the three types of loops?
In C and C++, including Arduino code, they are for, while and do...while, and Arduino's break reference (opens in a new tab) names all three. A for loop repeats a set number of times. A while loop checks its test first. A do...while loop checks at the end, so it always runs once. Python has only for and while.
What is a for loop in simple language?
A for loop is a counting loop: "do this a set number of times". It keeps a counter, checks it before each pass and adds 1 after each pass, so it knows when to stop.
What is the difference between a while loop and a for loop?
Use a for loop when you know how many times to repeat, such as "blink 3 times". Use a while loop when you only know the condition, such as "blink while the button is held". MDN's looping code guide (opens in a new tab) calls them largely interchangeable and suggests beginners start with for, because all three parts sit together in its brackets.
What is a do-while loop, with an example?
A do-while loop runs its body first and tests afterwards, so it always runs at least once. "Read the knob until it is turned up" is one, because you need a reading before you can test it.
What is an infinite loop, and how do you stop one?
An infinite loop is a loop whose stop rule never comes true. Devices like thermostats run one on purpose. If yours is a bug, fix the test so something changes it, or leave with break. In Scratch, click the stop sign. On the MakerBuddy board, press Stop on the rule's row.
Is there a while loop in Python?
Yes. The Python tutorial (opens in a new tab) introduces it with a short program that prints the Fibonacci numbers below 10. Python has no do-while loop, so programmers use while True: with break.
Can you learn loops without writing code?
Yes. Scratch has forever, repeat and repeat until blocks, and the MakerBuddy board has four loop modes you pick with one click. Each maps to a loop you will later write in code. Find more guides on the MakerBuddy blog.
Sources
- Oak National Academy: Count-controlled iteration (KS3 computing lesson) (opens in a new tab)
- MDN Web Docs: Looping code (opens in a new tab)
- Arduino Docs: for loop (opens in a new tab)
- Arduino Docs: while loop (opens in a new tab)
- Arduino Docs: do...while loop (opens in a new tab)
- Arduino Docs: break (opens in a new tab)
- Arduino Docs: loop() (opens in a new tab)
- Arduino Docs: millis() (opens in a new tab)
- Arduino Docs: Blink Without Delay (opens in a new tab)
- Python documentation: More control flow tools, for statements (opens in a new tab)
- Python documentation: First steps towards programming (opens in a new tab)
- Python language reference: Compound statements (opens in a new tab)
- Scratch Wiki: Forever block (opens in a new tab)
- Scratch Wiki: Repeat block (opens in a new tab)
- Scratch Wiki: Repeat Until block (opens in a new tab)
- CSTA K–12 Computer Science Standards, revised 2017 (PDF) (opens in a new tab)
- GOV.UK: National curriculum in England, computing programmes of study (opens in a new tab)




