ZachLabs Arduino

# Deep Dive: for Loops

In this lab’s example program we made an LED blink. But what if we wanted to blink the LED three times and then pause before starting over again? We could change our `loop()` function to the following:

``````void loop()
{
digitalWrite(2, HIGH);
delay(100);
digitalWrite(2, LOW);
delay(100);

digitalWrite(2, HIGH);
delay(100);
digitalWrite(2, LOW);
delay(100);

digitalWrite(2, HIGH);
delay(100);
digitalWrite(2, LOW);
delay(100);

delay(400);
}
``````

This would do exactly what we wanted but is verbose and difficult to change. What if we wanted to blink ten times instead of three? In C we can create loops that run a block of code multiple times, just like in Scratch and Python. Let us rewrite our code to use a for loop:

``````void loop()
{
for (int i = 0; i < 3; i++)
{
digitalWrite(2, HIGH);
delay(100);
digitalWrite(2, LOW);
delay(100);
}

delay(400);
}
``````

This code is more complex looking but makes it easier to change the number of blinks and their timing. The `for` loop in this example creates a variable called `i`, sets `i` to 0, and then blinks the LED 3 times because it increments `i` each time and stops when `i` reaches 3 (when `i < 3` is no longer true).

## The Structure of for Loops

The structure of `for` loops in C is more complex than in other programming languages. Once you know what you are looking at, however, they are not so bad. A `for` loop has 4 parts:

``````for (A; B; C)
{
D
}
``````
• (A) is the initializer of the loop and runs exactly once at the beginning of the loop. This is where you typically define and set an initial value for your loop variable.

• (B) is the condition of the loop and is checked each time the loop runs. When it is no longer true the loop will stop. If it is always true your loop will run forever and you will be stuck in an infinite loop. Your condition will almost always be a comparison involving your loop variable.

• (C) is run after each time through the loop and is typically used to modify the loop variable to get closer to a stopping point. In our example we used `i++`, which is equivalent to `i = i + 1`, or the slightly more compact `i += 1`. Any of these would increase `i` by one.

• (D) is the body of the loop and is code that will be run once each time the loop is run until the loop is finished.

## Using for Loops

This is still kind of confusing! An easy way to get started is to use the following template:

``````for (int i = N; i < M; i++)
{
}
``````

This will run the body of the loop M - N times, with `i` being set equal to each of the values N through M-1 in each of the times the body of the loop is run. All you need to do is specify the values of N, M, and the body.

``````for (int i = 2; i < 7; i++)
{
Serial.println(i);
}
``````

Running the above code will print 2, 3, 4, 5, and 6 to the serial monitor. Here is why:

(A) creates the loop variable `i` and sets it equal to 2. (B) checks whether `i` is less than 7 and, if it is (if that statement is true), then the code in the curly braces (D) is run once and, in this example, it prints the value of `i` to the serial monitor. After that, (C) adds one to the variable `i` and we loop back through (B), (D), and (C) until the condition in (B) is no longer true. When `i` equals 7, the loop stops because `i < 7` is now false.