Creative Coding · Session c05

Control Structures

Tell the computer to repeat things — with precision, with variations, with power.

So far every line of your code ran once. A loop changes that: it runs the same block of code many times, usually with a counter that changes each time. That counter is a variable you can use — to set positions, colours, sizes, anything.

This session gives you three experiments you can run live. Use them to build intuition before you see nested loops in c06.

The for loop — three parts, one line

for ( let i = 0 ; i < 8 ; i++ ) {
circle(20 + i * 50, 100, 30); // runs 8 times
}
① Initialiseruns once at the start
i starts at 0
② Conditionchecked before each run
stop when false
③ Updateruns after each loop
i grows by 1

The loop above draws 8 circles. On the first run, i = 0 → circle at x = 20. On the second, i = 1 → circle at x = 70. And so on until i = 8, which fails the condition i < 8 and stops the loop.

Experiment 1 — Step through a loop

Press Step → once for each iteration. Watch i grow, the condition get checked, and one circle appear. Change the limit with the slider, then reset and step through again.

Press Step → to start the loop.
8
for (let i = 0; i < 8; i++) { circle(20 + i * 50, 80, 32); }

The update step — four ways to change i

The third part of the for loop can do more than just add 1. Here are the four operator families, with shorthand for each:

OperatorMeansExample
i++i = i + 10, 1, 2, 3, 4 …count up by ones
i += 3i = i + 30, 3, 6, 9, 12 …count up by threes
i--i = i − 110, 9, 8, 7 …count down
i -= 10i = i − 10100, 90, 80 …count down by tens
i *= 2i = i × 21, 2, 4, 8, 16, 32 …double each time ← geometric!

Experiment 2 — Change the update step

Pick an operator below. See how the same canvas fills up differently — more circles, fewer circles, different spacing.


The while loop — when you don't know how many times

A for loop is perfect when you know the count up front. A while loop keeps going as long as a condition is true — useful when you stop based on a value, not a fixed count:

let size = 90; while (size > 3) { circle(210, 110, size); size = size * 0.6; // shrink by 40% each time }

You can't easily write this as a for loop, because you don't know in advance how many times it takes before size drops below 3.

Experiment 3 — Step through a while loop

Each press adds one ring. Watch the size variable shrink. Notice how many iterations it takes — the computer figures that out for you.

Press Step → to draw the first ring.

The counter IS the data.
The loop variable i isn't just a counter — it's a number you can feed into any formula. Use it as x position (i * 50), as a colour (i * 30 for hue), as a size (100 - i * 8), or all three at once. Every different formula gives a different visual from the same loop.
Connection to sequences: i++ generates an arithmetic sequence (constant difference = 1). i *= 2 generates a geometric sequence (constant ratio = 2). The loops you just experimented with are the same patterns from Lessons 8 and 9 — just running at computer speed.
Challenges
  1. In Experiment 1, set the limit to 14. How many circles appear? Step through slowly — where does the circle go off the right edge of the canvas? Why?
  2. In Experiment 2, switch to i *= 2 (from 1). Only 9 circles appear even though i shoots up to 256. Try to predict: what is i on each circle? (Hint: 1, 2, 4, …)
  3. Change the for loop code (in your own p5 editor) to colour each circle differently: fill(i * 30, 100, 200). What changes?
  4. Write a while loop (in your editor) that starts at let n = 1 and doubles it each time (n *= 2) until n > 500. Draw a circle of diameter n at the centre. How many circles appear?
  5. Open challenge: Combine a loop with the mouse: inside a for loop, draw lines from mouseX, mouseY to (i * 42, 0) — one line per value of i. Move the mouse. What happens?
Next session → c06: Loops + Patterns Now you know exactly how a for loop works. In c06, you'll put one loop inside another — a nested loop — to visit every cell of a grid and fill it with colour. 10 columns × 7 rows = 70 squares, all in about 10 lines of code.
← All lessons
Focus 25:00

Ask your tutor