Loops: Repeating a Sequence of Commands

Every arduino program contains two functions:

This packet is not about the function in your sketch named loop,
but about any block of statements that should run again and again
util some condition is met. Such blocks are called “loops”. They take
one of two forms:

for Loops

Use a for loop to execute a statement (or a block of statements) a certain number
of times, or until some condition is met.

Anatomy of a for loop

The graphic above illustrates the three aspects of setting up
a for loop:

Initialization (Set the Start condition): int i=0
In this case, a counter was declared and initialized to 0.
Termination (Test the Stop condition): i < 3
In this case, the termination statement tested whether the counter is less than 3.
The loop stops executing when this statement is true. Any of the boolean
tests described in the packet on conditionals
statements can be used in the termination test.
Iteration (To-Do after every iteration): i = i + 1
In this case, the counter is incremented by 1 just after every execution of
all the statements in the block.

The programmer has a lot of freedom in choosing:
which variable(s) to initialize and what initial value to give them,
what statement(s) are executed within the loop
* what test(s) are used to determine whether the loop should be exited.

For example, this loop causes a function blink() to be called
again and again, forever, since there is no termination test:

for ( ; ; ;) {
    blink();
}

The next loop causes the function blink() to execute until the delay time
falls below a limit:

for (delayTime = 1000; delaytime > 20; delayTime = delaytime * 0.5) {
    blink(RLED);
    blink(YLED);
}

while Loops

A block of statements can also be repeated under the control of while.

The keyword while is followed by an expression in parentheses which evaluates to True or False.

A while loop is completely equivalent to a for loop if:

The figure below shows a for loop and its equivalent while loop:

A for loop An equivalent while loop