![Thames & Kosmos Code Gamer Experiment Manual Download Page 33](http://html1.mh-extra.com/html/thames-and-kosmos/code-gamer/code-gamer_experiment-manual_1099224033.webp)
Here, the loop starts with 2 and
i = i + 2
raises the
counter variable by 2 after each pass through the loop.
Here, the loop starts with the starting value of 1. This time,
instead of testing whether
i
is less than 42, it tests
whether
i
is less than or equal to 42. That means that the
loop is also passed through one more time for the value of
42. But it also works in the opposite direction:
// Outputs the numbers 42, 41, 40, ..., 1:
for (int i = 42; i >= 1; --i) {
Serial.println(i);
}
The loop starts with the value of 42. It keeps running
through as long as
i
has a value that is greater or equal
to 1. The
--i
means that
i
is reduced by 1 after each
pass through the loop. In other words, it counts
backwards.
// Outputs the numbers 2, 4, 6, ..., 42:
for (int i = 2; i <= 42; i = i + 2) {
Serial.println(i);
}
You can actually use any instructions you like in place of
++i
or
--i
. For example, you can just use even numbers:
31
THE FOR LOOP
CodeGamer
CodeGamer manual inside english.indd 31
7/19/16 12:32 PM