2. Objectives
Describe the purpose of loops.
Use for loops.
Use while loops.
Use do while loops.
Use the break and continue statements.
Nest loops.
3. The for Loop
The for loop repeats one or more
statements a specified number of times.
Like an if statement, the for loop uses
parentheses.
In the parentheses are three items
called parameters, which are needed to
make a for loop work.
4. Example for loop
for(i = 1; i <= 3; i++)
{ Serial.print(i); }
The first parameter, called the initializing
expression, initializes the counter variable.
The second parameter is the expression that
will end the loop, called the control
expression. It is tested before the loop.
The third parameter is the step expression. It
changes the counter variable, usually by
adding to it.
6. while loops
A while loop is similar to a for loop, but
in a while loop, something inside the
loop triggers the loop to stop.
There are two kinds of while loops: the
standard while loop and the do while
loop.
The difference between the two is
where the control expression is tested.
7. The while Loop
The while loop repeats a statement or group
of statements as long as a control expression
is true.
Unlike a for loop, a while loop does not use a
counter variable.
The control expression in a while loop can be
any valid expression.
In a while loop, the control expression is
tested before the statements in the loop
begin.
8. Example of a while loop
while (num > 1.0)
{
Serial.print(i);;
num = num / 2;
}
As long as the value of num is greater
than 1.0, the loop will continue.
9. The do while Loop
A do while loop repeats a statement or
group of statements as long as a
control expression is true at the end of
the loop.
Because the control expression is tested
at the end of the loop, a do while loop
is executed at least one time.
10. Example of a do while loop
int num =1, i=0;
do
{
Serial.print(i);
i++;
if(i==3){num = 0)}
}
while (num != 0);
int num =1, i=0;
while (num != 0)
{
Serial.print(i);
i++;
if(i==3){num = 0)}
}
11. Stopping in the Middle of a Loop
The keyword break, also utilized with
switch statements, can be used to end
a loop before the conditions of the
control expression are met.
The continue statement skips the
remainder of a loop and starts the next
iteration of the loop.
12. Nesting Loops
Using nested loops is possible and very
common.
We typically refer to the loops as the
inner loop and the outer loop.
Make sure to trace the steps of the
program carefully to understand how
nested loops behave.
13. Summary
A loop is used to cause a program to repeat a
group of statements a given number of times.
Loops are iteration structures.
Each loop through a group of statements is
called an iteration.
A for loop repeats one or more statements a
specified number of times.
A for loop uses three parameters to control
the loop.
14. Summary
Step expressions can be simple or complex.
Braces group the statements in a loop.
A while loop repeats a statement or group of
statements as long as a control expression is
true. The control expression is tested at the
top of the loop.
A do while loop repeats a statement or group
of statements as long as a control expression
is true at the end of the loop.
15. Summary
The break keyword ends a loop before the
conditions in the control expression are met.
The continue keyword skips the remainder of
the statements in the loop and continues with
the next iteration of the loop.
Loops may be nested to have loops inside
loops.
16. Quiz
Create a for loop, while loop and do while
statement that displays the following:
1) 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
55
2) 5, 6, 7, 8, 9, 10
45
3) 1000, 750, 500, 250
2500
17. int count;
int sum = 0;
for(count = 10; count > 0; count--) {
sum = sum + count;
Serial.print(count);
Serial.print(“, ”);
}
Serial.println(sum);