Loops¶
Already, you’ve learned how to write functions that can be reused to perform a specific task without repeating code. But, what if you needed to perform this task multiple times? This is where loops come in.
For example, imagine you want to blink an LED on and off 10 times. You might write something like this:
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
// ...repeat eight more times
It gets the job done, but that’s a lot of repetitive code! It’s not just tedious—it’s also inefficient. What if you wanted to blink the LED 100 times? Or change the timing later? You’d have to rewrite or modify the same lines repeatedly, which makes your code harder to manage and more prone to errors.
Loops let you write the repetitive part of your program once and have the Arduino handle the repetition for you. They’re like your code’s personal assistant, taking care of the repetitive grunt work so you can focus on the bigger picture.
Using loops, we can easily repeat the blinking of an LED without having to write the same lines of code over and over:
for (int i = 0; i < 10; i++) {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}
Much cleaner, right? Not only is the code easier to read, but it’s also more flexible—you can change the number of repetitions (10 in this case) just by updating one number.
Every time the loop runs it is called an iteration. In this example, the loop runs 10 times, so it has 10 iterations. This is the power of loops: they let you repeat a block of code as many times as you need without having to write it out each time.
But how do they work?
for Loops¶
The for loop is a powerful tool when you know how many times you
want to repeat something.
The for loop uses a special variable (called a loop variable)
that you create to count how many times the loop has run. You name this
variable yourself (like i, count, or anything else), and the
loop updates it automatically during each repetition.
How for Loops Work¶
A for loop has 3 main parts:
Initialization: Sets a starting point for your loop.
Condition: Checks whether the loop should continue.
Increment/Decrement: Updates the loop variable after each iteration.
for Loop Syntax¶
for (initialization; condition; increment/decrement) {
// Code to execute
}
In this syntax:
Initialization: Sets the loop variable to an initial value. This is usually where you create the loop variable and set its starting value.
Condition: Checks whether the loop should continue. If the condition is true, the loop runs; if it’s false, the loop stops.
Increment/Decrement: Updates the loop variable after each iteration. This is where you increase or decrease the loop variable to move the loop forward.
The loop variable is only available in the loop itself. Once the loop finishes, the variable is no longer accessible. Ie. the scope of the loop variable is limited to the loop itself.
for (int i = 0; i < 10; i++) {
// `i` is only available within the loop
Serial.println(i);
}
Serial.println(i); // This will cause an error because `i` is not available here
for Loop Example¶
LED Example¶
For example, you can use a for loop to turn an LED on and off 5
times:
// Turn on LEDs connected to pins 2 through 6
for (int pin = 2; pin <= 6; pin++) {
digitalWrite(pin, HIGH); // Turn on the LED
delay(500); // Wait for half a second
digitalWrite(pin, LOW); // Turn off the LED
}
Here, the for loop iterates (cycles) through pin numbers 2 to 6,
turning on each LED.
Summing Numbers Example¶
As another example, let’s say you wanted to add all the numbers in
an array together. You could use a for loop to iterate through the
array and add each number to a total:
int numbers[] = {1, 2, 3, 4, 5};
int total = 0;
for (int i = 0; i < 5; i++) {
total += numbers[i]; // Add the current number to the total
}
Serial.println(total); // Print the total
>>> 15
Fibonacci Example¶
What if you wanted to store the first 10 fibonacci numbers in an array. You could use a for loop to iterate through the array and calculate each number instead of manually writing each number:
int fibonacci[10];
fibonacci[0] = 0;
fibonacci[1] = 1;
for (int i = 2; i < 10; i++) {
fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
}
for (int i = 0; i < 10; i++) {
Serial.println(fibonacci[i]);
}
>>> 0
>>> 1
>>> 1
>>> 2
>>> 3
>>> 5
>>> ...
But what if the amount of fibonacci numbers you wanted to calculate changes to 20? What if it changed to 50? You can update your program to automatically account for this by changing the size of an array and the loop condition with a common variable.
const int total_fib_numbers = 30; // Constant variable to store the total number of fibonacci numbers to calculate
int fibonacci[total_fib_numbers];
fibonacci[0] = 0;
fibonacci[1] = 1;
for (int i = 2; i < total_fib_numbers; i++) {
fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
}
for (int i = 0; i < total_fib_numbers; i++) {
Serial.println(fibonacci[i]);
}
>>> 0
>>> 1
>>> 1
>>> 2
>>> 3
>>> ...
while loops¶
A while loop is ideal for situations where you don’t know in advance
how many times a task needs to repeat. Unlike a for loop, which runs
a set number of times, a while loop keeps going as long as its
condition evaluates to true.
The key feature of a while loop is its condition—a boolean
expression that is checked at the start of each loop iteration. If the
condition is true, the loop runs; if it’s false, the loop stops.
This makes while loops great for tasks where the stopping point
depends on a dynamic or unpredictable factor, like user input or sensor
readings.
How while Loops Work¶
A while loop has 2 main parts:
Condition: The loop checks a condition before every iteration. If the condition is false, the loop exits immediately.
Repetition: If the condition is true, the code inside the loop executes and then rechecks the condition.
While Loop Syntax¶
while (condition) {
// Code to execute
}
While Loop Example 2: Countdown Timer¶
We can also use a while loop to create a countdown timer. For
example, let’s count down from 10 to 1, printing each number to the
Serial Monitor and then printing “Liftoff!” when the countdown reaches
0.
void start() {
Serial.begin(9600);
// A variable denoting where we start our countdown.
int countdown = 10;
while (countdown > 0) { // Keep looping until the countdown reaches 0
Serial.println(countdown); // Print the current countdown value
delay(1000); // Wait 1 second
countdown--; // Decrease the countdown by 1
}
Serial.println("Liftoff!");
}
void loop() {
// Nothing to do here
}
Here:
The loop starts with
countdown = 10and repeats untilcountdown > 0is false.On each iteration, the value of
countdowndecreases by 1.
Tip
If you haven’t noticed, this example can also be done with a for
loop! The choice between for and while loops depends on the
specific task you’re trying to accomplish.
for (int i = 10; i > 0; i--) { // From 10 to 1
Serial.println(i); // Print the current countdown value
delay(1000); // Wait 1 second
}
While Loop Key Points¶
Make sure the condition will eventually become false; otherwise, the loop will run forever (infinite loop). For example:
while (true) { // This will run forever unless you break the loop manually. // Our program won't do anything else until you reset it. }
Use a delay or modify the condition inside the loop to prevent unnecessary CPU usage or infinite looping.
With while loops, you have flexibility for dynamic, real-time
decision-making, making them powerful for tasks like waiting for an
input or monitoring a sensor.
Why Use Loops?¶
Think of loops as the “secret sauce” to efficient coding. They save you time, reduce errors, and make your code adaptable to change.
for Loops: Use these when you know in advance how many times you want to repeat something, like iterating through an array or cycling through a fixed number of pins.
while Loops: These are ideal for conditions that depend on real-time input, such as waiting for a sensor to detect a specific value or monitoring a button press.
Break and Continue Statements¶
Sometimes you need to break out of a loop early or skip an iteration
based on a specific condition. This is where break and
continue statements come in.
break: Exits the loop immediately, regardless of the loop condition.
continue: Skips the rest of the current iteration and moves to the next one.
These statements give you more control over the flow of your loops, allowing you to fine-tune your code based on specific conditions.
Break Statement Example¶
Let’s say we wanted to continue looping until we found a specific
number, then exit the loop early. We can use the break statement to
do this.
int number_to_find = 5;
for (int i = 0; i < 10; i++) {
Serial.println(i);
if (i == number_to_find) {
Serial.println("Number found!");
break; // Exit the loop early
}
}
Serial.println("Loop finished!");
In this example, the loop is set to run from 0 to 9, printing
each number. When i equals number_to_find (5), the loop exits
early with the break statement. The program then prints “Number
found!” and “Loop finished!”.
So, when you run this program, you’ll see:
0
1
2
3
4
5
Number found!
Loop finished!
Continue Statement Example¶
continue is similar to break, however, continue will simply
skip to the next loop iteration instead of stopping the loop. Let’s say
that we hate ANY number that ends in 5. We can use the continue
to skip any number that ends in 5.
int number_we_hate = 5;
for (int i = 0; i < 10; i++) {
if (i % 10 == number_we_hate) {
Serial.println("We hate number: " + i);
continue; // Skip this iteration
}
Serial.println(i);
}
This program will print every number from 0 to 9, except for
5. When you run this program, you’ll see:
0
1
2
3
4
We hate number: 5
6
7
8
9
Note
Note how the use of the modulus (%) operator is used here. As
mentioned in Math Operations, the modulus
operator returns the remainder of a division operation. Consider if
we made the loop go all the way to 20 instead of 10. When we hit 15,
15 % 10 is 5, so the program would skip printing 15 as
well. The same would happen for 25, 35, etc.
You can use continue and break with both for and while
loops. These statements give you more control over the flow of your
loops, allowing you to fine-tune your code based on specific conditions.
Loops vs. loop()¶
The loop() function and for / while loops serve different purposes in
Arduino programming.
The
loop()function is a special system function that runs indefinitely on your Arduino board, cycling through its code block as long as the board has power. It handles the overarching repetition of your program.void loop() { // This is the `loop()` function that runs indefinitely. }
A
fororwhileloop, on the other hand, performs controlled repetitions of specific tasks within theloop()function or elsewhere in your code.void some_other_function() { // This is a separate function that you can call from `loop()` for (int i = 0; i < 3; i++) { Serial.println(i); // Prints 0, 1, 2 } } void loop() { // This is the `loop()` function that runs indefinitely. // We can call `some_other_function()` here that has a `for` loop some_other_function(); // We can also have a `for` loop directly within `loop()` // Example of a `for` loop within `loop()` for (int i = 0; i < 3; i++) { Serial.println(i); // Prints 0, 1, 2 } // Example of a `while` loop within `loop()` int x = 0; while (x < 3) { Serial.println(x); // Prints 0, 1, 2 x++; } }