Notes Arduino 8
Notes Arduino 8
Serial.begin(9600);
void loop() {
}
while Loop Structure
The while loop has a structure as follows:
while (loop test expression goes here) {
Statements that run in the loop go here
Statement 1
Statement 2
...
}
The while loop starts with the while keyword followed by a test expression between
opening and closing parentheses. Opening and closing braces denote the body of the
loop.
Test Expression
As with the for loop, the while loop has a test expression that will determine whether the
statements in the loop will run or not. If the test expression evaluates to true, the loop
statements are run. If the test expression evaluates to false, the loop statements will not
be run, but the statements that follow the closing brace of the loop will be run – i.e.
execution continues outside and below the loop.
Initialize Expression
The for loop had an initialize expression as part of the loop. The while loop can use any
variable from the sketch that contains a valid value. In the example sketch, the variable
used in the loop (i) must be initialized when it is defined, otherwise it will contain any
random value.
Increment Expression
An increment expression was used in the for loop examples in the previous part of this
course. In the while loop example, the increment expression is placed inside the loop
body.
How the while Loop Example Works
In the example sketch, the following happens:
Serial.begin(9600);
// count up to 25 in 5s
while (sum < 25) {
sum = sum + 5;
Serial.print("sum = ");
Serial.println(sum);
delay(500); // 500ms delay
}
}
void loop() {
}
The video below shows the sketch running.
In this sketch, a variable called sum is defined and initialized to 0. The test expression in
the while loop tests if sum contains a value less than 25.
Inside the loop, the sum variable is incremented by 5 each time through the loop by the
arithmetic expression:
sum = sum + 5;
Although the test expression will evaluate to false when sum == 25, 25 is still the last
number that is printed. This is because the last time that the test expression evaluates to
true is when sum == 20, but sum is then incremented to 25 and printed before the test
expression is evaluated again.
Serial.begin(9600);
// count up to 25 in 5s
do {
sum = sum + 5;
Serial.print("sum = ");
Serial.println(sum);
delay(500); // 500ms delay
} while (sum < 25);
}
void loop() {
}
All the statements are run in the loop body before the test expression is evaluated.
If sum is initialized to a value of 25 when it is defined, as shown in the sketch below, the
loop will run once and 30 will be printed. The loop will then not run again because the test
expression evaluates to false.
void setup() {
int sum = 25;
Serial.begin(9600);
// count up to 25 in 5s
do {
sum = sum + 5;
Serial.print("sum = ");
Serial.println(sum);
delay(500); // 500ms delay
} while (sum < 25);
}
void loop() {
}
Using the same sketch, but changing the do while loop to a while loop, as shown below,
the statements in the loop body will never run. This is because the test expression is
evaluated before executing statements in the loop body. The test expression immediately
evaluates to false, so the loop statements will never run.
void setup() {
int sum = 25;
Serial.begin(9600);
// count up to 25 in 5s
while (sum < 25) {
sum = sum + 5;
Serial.print("sum = ");
Serial.println(sum);
delay(500); // 500ms delay
}
}
void loop() {
}
In the above example, no output will be seen in the serial monitor window when the sketch
is run. The while loop evaluates to false and then execution drops straight into the empty
main Arduino loop.