Loops
Repeat something ten times, or until something changes, without copying and pasting.
In 10 seconds
- • for repeats a known number of times.
- • while repeats until something changes.
- • loop() is already an endless loop.
Computers are extraordinarily good at doing boring things over and over. Loops are how you ask them to. If you ever find yourself copying and pasting the same three lines with slightly different numbers, a loop is what you actually wanted.
The for loop: repeat a known number of times
A for loop has three parts inside its brackets, separated by semicolons: where to start, how long to keep going, and what to change each time round. Something like for (int i = 0; i < 8; i++) reads as "start i at zero, keep going while i is less than eight, and add one to i each time". The block inside runs eight times, with i being 0, 1, 2, 3, 4, 5, 6, 7.
Notice it stops at 7, not 8. Counting from zero feels strange for about a week and then feels completely natural, mostly because arrays are numbered the same way — the first item in an eight-item array is number 0 and the last is number 7.
The counter is not just a tally; you can use it. Loop over an array of LED pins and i picks which LED to light. Loop from 0 to 255 and use i as the brightness, and you have written a fade.
The while loop: repeat until something changes
Sometimes you do not know how many times. A while loop keeps going as long as its condition is true: "while the button is still held down, keep the motor running". It checks the condition, runs the block, checks again.
Be careful here. If nothing inside the loop can ever make the condition false, the program becomes stuck forever and stops responding to everything else. Writing while (true) with no way out is the classic way to make an Arduino appear dead.
loop() is a loop too
It is worth remembering that your whole program already sits inside a loop. Often the neatest solution is not to write a while loop at all, but to let loop() handle the repetition naturally and simply check the condition each time round. Code written this way stays responsive to everything else, which matters enormously once your project does more than one thing.
Escaping early
breakjumps out of the loop immediately.continueskips the rest of this round and starts the next one.
Use them sparingly — a loop with five escape hatches is harder to follow than one with a clear condition — but for stopping a search the moment you find what you want, break is perfect.
Loops and delay do not mix well
A for loop full of delay() calls locks up the whole board while it runs. That is fine for a simple animation, but the moment your project also needs to watch a button, you have a problem: the button press happens during the delay and is missed entirely. The grown-up solution is to track time with millis() and let loop() do the repeating, which is exactly what the Mini Smart Home project demonstrates.
Quick quiz: Loops
3 questions to check it stuck.
Next lesson
🧰 Functions
Ready to try it for real?
Reading is good. Wiring is better. Pick a project and put this to work.
Explore projects