Thames & Kosmos Code Gamer Experiment Manual page 33

Coding workshop ? game with kosmobits
Table of Contents

Advertisement

THE FOR LOOP
Here, the loop starts with the starting value of 1. This time,
instead of testing whether
whether
is less than or equal to 42. That means that the
i
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
to 1. The
means that
--i
pass through the loop. In other words, it counts
backwards.
CodeGamer manual inside english.indd 31
is less than 42, it tests
i
has a value that is greater or equal
is reduced by 1 after each
i
You can actually use any instructions you like in place of
or
. For example, you can just use even numbers:
++i
--i
// Outputs the numbers 2, 4, 6, ..., 42:
for (int i = 2; i <= 42; i = i + 2) {
Serial.println(i);
}
Here, the loop starts with 2 and
counter variable by 2 after each pass through the loop.
CodeGamer
raises the
i = i + 2
31
7/19/16 12:32 PM

Advertisement

Table of Contents
loading

Table of Contents