Arduino IDE: for loops against while / do while #6

Arduino IDE

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:

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:

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:

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:

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:

Arduino for loops and RGB LED

Previous articles:

Follow us to keep yourself updated with the news!

Simone Candido è un ragazzo appassionato del mondo tech nella sua totalità. Simone ama immedesimarsi in nuove esperienze, la sua filosofia si basa sulla irrefrenabile voglia di ampliare a 360° le sue conoscenze abbracciando tutti i campi del sapere, in quanto ritiene che il sapere umano sia il connubio perfetto tra cultura umanistica e scientifica.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.