Welcome back to our programming tutorial using the Arduino IDE. Today we will deal with the for loops, and their use with while and do while loops.
You can take a look at the previous chapters of the course here:
- Arduino IDE: while and do while loops #5
- Arduino IDE: turn on LEDs using a button (if) #4.1
- Arduino IDE: the conditional construction IF #4
- Arduino IDE: arithmetic and logical operators #3
- Arduino IDE: variables, constants and macros #2
- How can I use Arduino IDE to write a program? #1
- Arduino IDE: what it is and how it works #0
When you try to solve a problem, you can deal with an algorithm which repeats some of the instructions during execution. If you want to achieve the repetition of a group of instructions for a predefined number of cycles, you can use the for loop.
The for loop uses a repeating structure with a counter (or enumerative iteration).This structure allows to repeat a certain group of statements, on the basis of a countable number of times instead of the truth value of a condition.
The variable is called a counter, because it has the function of counting the number of repetitions.
The syntax of the for loop is the following:
1 2 3 |
for (int i=min; i<=max; i++){ repeated_code; } |
The repeated_code inside the for loop is executed max times. The instructions are repeated as many times as necessary to bring the counter i from the initial value (min) to the final value (max), increasing it by 1 at each repetition.
The counter must be a declared variable of type int. The declaration can be placed inside the for loop or previously.
Consider that if you instantiate the type of the variable inside the for loop, as the previous example, the variable life will be limited by the time of execution of the loop, and the variable will be destroyed at the end of the loop. If you need a more persistent variable that exists outside the loop, you should instead apply the following syntax:
1 2 3 4 5 6 |
int i; for (i=min; i<=max; i++){ repeated_code; } // Here you can reuse your i variable |
But what happens inside the for loop? How is it going? The syntax can be schematized in the following way, in order to better understand the characteristics:
1 2 3 |
for (initialization; condition; iteration ){ instructions; } |
Initialization, condition, and iteration expressions must be separated by semicolons (;).
The first expression consists of Initialization: this operation takes place only once, when the for loop is starting.
The second expression consists of the condition(s): when the condition(s) are true, the code will be executed.
The third expression relies on the iterative count: it is usually an instruction that increases or decreases the counter. When the (second) condition becomes false, the instructions inside the loop block will not be executed and the program will jump out of the loop.
Note that the condition clause is dynamic, and varies with the value of the counter: the counter is automatically updated at each cycle of the loop.
The third clause is usually updated by 1 at each cycle, but that’s not always the case: we can see iteration counters proceeding at different steps, both positive and negative.
The control of the repetition of instructions using a counter can be achieved in an equivalent way using a post-conditional repetition structure (do … while) or a pre-conditional structure (while).
The for structure is more compact and simpler to represent than the while structure, whenever we know beforehand how many times we need to repeat the instructions inside the loop.
In fact, in principle, any algorithm that uses a for loop can be transcribed into a form that uses only the while loop, and vice versa.
In the next article I will present a project with Arduino using an RGB LED with its color changing programmatically using loops. Here is a draft:
Previous articles:
- Arduino IDE: while and do while loops #5
- Arduino IDE: turn on LEDs using a button (if) #4.1
- Arduino IDE: the conditional construction IF #4
- Arduino IDE: arithmetic and logical operators #3
- Arduino IDE: variables, constants and macros #2
- Arduino IDE: how can I use Arduino IDE to write a program? #1
- Arduino IDE: what it is and how it works #0
Follow us to keep yourself updated with the news!