Making smart loops for repetitive tasks
A loop is a series of events repeating themselves in time. Basically, computers have been designed, at first, to make a lot of calculations repeatedly to save human's time. Designing a loop to repeat tasks that have to be repeated seems a natural idea. C natively implements some ways to design loops. Arduino core naturally includes three loop structures:
for
while
do
…while
for loop structure
The for
loop statement is quite easy to use. It is based on, at least, one counter starting from a particular value you define, and increments or decrements it until another defined value. The syntax is:
for (declaration & definition ; condition ; increment) { // statements }
The counter is also named index
. I'm showing you a real example here:
for (int i = 0 ; i < 100 ; i++) { println(i); }
This basic example defines a loop that prints all integers from 0
to 99
. The declaration/definition of the integer type variable i
is the first element of the for
structure...