The while loop is similar to the for loop that was explained in the previous part of this Arduino programming course. The main difference is that the while loop separates the elements of the for loop as will be shown.
Another loop called the do while loop is also covered. The do while loop is always run at least once before any tests are done that could break program execution out of the loop.
The while Loop
The sketch that follows does exactly the same as the for loop sketch from part 7 of this course, except that it uses the while loop so that we can see the similarities between the two loops.
void setup() { int i = 0; Serial.begin(9600); while (i < 10) { Serial.print("i = "); Serial.println(i); i++; } } 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:
Only when the variable i has been incremented to 10, will the loop expression evaluate to false and the loop will be exited.
while Loop Example 2
In the example sketch below, the while loop is used to count up to twenty-five in fives by adding five to a variable each time through the loop.
void setup() { int sum = 0; 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;
This expression means "add 5 to the sum variable".
The value that the sum variable holds is then printed out, followed by a half-second delay.
Because the value of the variable is first incremented and then printed out, we see the value 5 printed first and not the value of 0 that it was initialized to.
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.
The do while Loop
The do while loop works in the same way as the while loop, except that it always runs once even if the test expression evaluates to false.
do while Loop Structure
The do while loop consists of two keywords do and while, as shown below.
do { Statements that run in the loop go here Statement 1 Statement 2 ... } while (test expression goes here);
The body of the do while loop falls between opening and closing braces and contains statements that are to be run in the loop.
The while keyword and test expression come after the body of the loop and are terminated by a semicolon (;).
do while Loop Example
This example demonstrates the do while loop.
void setup() { int sum = 0; 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
No comments:
Post a Comment