Level 2Unit A · Session 4
4

Unit A · From blocks to text

Loops

What we are making: an LED that blinks a set number of times, then stops — which the forever block could never do on its own.

The idea

You know two loops from last year: repeat, which counts, and forever, which does not stop. C++ has both, and one of them is already doing its job invisibly.

The block you already know
The line that does the same thing
forever
void loop() {
(the blocks inside)
// you are already inside forever
}
repeat 5
for (int i = 0; i < 5; i++) {
(the blocks inside)
// this runs five times
}
loop() is the forever block. The for loop is repeat, with its counter written out.

The for line looks like a lot at first. It is three small things separated by semicolons, and it is always the same shape.

The three parts of a for loop
1for (int i = 0; i < 5; i++) {
2 // do the thing
3}
  • int i = 0 — start a counter at zero. Done once, before anything.
  • i < 5 — keep going while this is true. Checked before every round.
  • i++ — add one to i. Done at the end of every round.
💡 Tip
Counting from 0 is not a mistake. i < 5 with i starting at 0 gives 0, 1, 2, 3, 4 — five rounds. Programmers count from zero almost everywhere, and you will meet it again with arrays.

The code

Five quick blinks, then a pause
1int ledPin = 9;
2 
3void setup() {
4 pinMode(ledPin, OUTPUT);
5 Serial.begin(9600);
6}
7 
8void loop() {
9 for (int i = 0; i < 5; i++) {
10 digitalWrite(ledPin, HIGH);
11 delay(200);
12 digitalWrite(ledPin, LOW);
13 delay(200);
14 Serial.println(i);
15 }
16 
17 delay(3000);
18}
  • Line 9 — the for loop. Everything in its curly brackets happens five times.
  • Line 14 — printing i so you can watch the counter. Open the serial monitor.
  • Line 17 — this is outside the for loop but inside loop(), so it runs once per group of five.
  1. Upload. Open the serial monitor (the magnifying glass, top right). Set it to 9600.
  2. Watch: 0 1 2 3 4, pause, 0 1 2 3 4, pause.
  3. Change i < 5 to i < 10. What happens?
  4. Move delay(3000) inside the for loop's brackets. Upload. Why is it so different?
⚠ Careful
That last step is the whole lesson about brackets. Inside or outside the curly brackets changes everything, exactly the way inside or outside the C-shape did last year.

The other one: while

while repeats as long as something is true. It has no counter of its own.

The block you already know
The line that does the same thing
repeat until button pressed
while (digitalRead(2) == LOW) {
(the blocks inside)
// keep going until it is pressed
}
repeat-until, the other way round: while means 'keep going while'.
💡 Tip
Use for when you know how many times. Use while when you do not. That one sentence covers nearly every case you will meet this year.
⚠ Careful
A while whose condition never becomes false runs forever and nothing after it ever happens. If your board seems to freeze, look at your while loops first.

Try it

  1. Make the LED blink faster each time round the for loop. Hint: use i in the delay.
  2. Put a for loop inside another for loop. Print both counters and work out the order they change in.
  3. Use a while to hold the program until the button is pressed, then blink five times.

What you learned

  • loop() is the forever block — you are always already inside it.
  • for counts: start, keep-going-while, and step.
  • Counting from 0 is normal.
  • while repeats without counting.
  • Inside or outside the curly brackets changes everything.
Challenge optional — only if you finish early
  • Write a loop that blinks 5 times, pauses, then blinks 5 more — without repeating any code.
3. Variables