Hardware Reference
In-Depth Information
do {
execute this code once and then continue executing it
repeatedly as long as the condition is true
} while (condition)
This means that the code inside a do... while block will always execute at
least once.
for Loops
A for loop is a way of having a block of code execute a specific number of
times. The syntax is a bit complicated at first, but you'll find yourself using
them and encountering them a lot more than while and do... while loops.
With practice, you'll be writing and reading for loops fluently.
Let's start by taking a look at a basic for loop:
for (int i = 0; i < 10; i++) {
Serial.println("Hello, Galileo!");
}
The code above would print “Hello, Galileo!” ten times to the serial monitor.
The for loop statement has three parts inside the parentheses and each part
is separated by a semicolon. First the initialization , which is code that is ex-
ecuted once before anything else happens. The second part is the condi-
tion . It's evaluated and if the statement is true, then the code in the block is
executed. After the code in the block is executed, the third part of code in the
for loop statement, the afterthought , is executed. To summarize the syntax:
for (initialization; condition; afterthought) {
execute this code if condition is true
}
To explore what's going on, take a look at another for loop:
for (int i = 0; i < 3; i++) {
Serial.print("Loop iteration number: ");
Serial.println(i);
}
This would print:
Loop iteration number: 0
Loop iteration number: 1
Loop iteration number: 2
Here's a breakdown of what's going on, to explain how that for loop works:
Search WWH ::




Custom Search