SlideShare a Scribd company logo
4
Most read
7
Most read
8
Most read
Loops
Objectives
 Describe the purpose of loops.
 Use for loops.
 Use while loops.
 Use do while loops.
 Use the break and continue statements.
 Nest loops.
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.
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.
Other step expressions
Countdown
for(i = 20; i >= 0; i--)
{Serial.print(i); }
Count by 10s
for(i = 10; i <= 300; i=i+10)
{Serial.print(i); }
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.
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.
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.
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.
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)}
}
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.
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.
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.
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.
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.
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
 int count;
 int sum = 0;
 for(count = 10; count > 0; count--) {
 sum = sum + count;
 Serial.print(count);
 Serial.print(“, ”);
 }
 Serial.println(sum);

More Related Content

PPTX
While , For , Do-While Loop
PDF
PPTX
NUMERICAL METHODS WITH MATLAB PROGRAMMING
DOCX
Loops and iteration.docx
PDF
Chapter 3 - Flow of Control Part II.pdf
PPTX
presentation on powerpoint template.pptx
PPTX
130707833146508191
DOCX
loops and iteration.docx
While , For , Do-While Loop
NUMERICAL METHODS WITH MATLAB PROGRAMMING
Loops and iteration.docx
Chapter 3 - Flow of Control Part II.pdf
presentation on powerpoint template.pptx
130707833146508191
loops and iteration.docx

Similar to Loops Do While Arduino Programming Robotics (20)

PPT
Programming loop
PPTX
Loops in python including control statements and various test cases
PPTX
Loops in python including control statements and various test cases
PPTX
Loops c++
PDF
Loops in c++
PPTX
Chapter 5 java
PPT
Decision making and looping
PDF
Notes2
PPTX
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
PPTX
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
PPTX
Week 4.pptx computational thinking and programming
PPTX
Comp ppt (1)
PDF
Java Repetiotion Statements
PPT
M C6java6
PDF
Chapter 12 Computer Science ( ICS 12).pdf
PPTX
Loops in c programming
PPS
Programming in Arduino (Part 2)
PDF
Looping in c language
PDF
Python_Module_2.pdf
Programming loop
Loops in python including control statements and various test cases
Loops in python including control statements and various test cases
Loops c++
Loops in c++
Chapter 5 java
Decision making and looping
Notes2
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Week 4.pptx computational thinking and programming
Comp ppt (1)
Java Repetiotion Statements
M C6java6
Chapter 12 Computer Science ( ICS 12).pdf
Loops in c programming
Programming in Arduino (Part 2)
Looping in c language
Python_Module_2.pdf
Ad

Recently uploaded (20)

PDF
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
PDF
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PDF
LDMMIA Reiki Yoga S2 L3 Vod Sample Preview
PDF
LDMMIA Reiki Yoga Workshop 15 MidTerm Review
PDF
5.Universal-Franchise-and-Indias-Electoral-System.pdfppt/pdf/8th class social...
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PPTX
Software Engineering BSC DS UNIT 1 .pptx
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
PPTX
Strengthening open access through collaboration: building connections with OP...
PDF
Module 3: Health Systems Tutorial Slides S2 2025
PDF
Landforms and landscapes data surprise preview
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
UPPER GASTRO INTESTINAL DISORDER.docx
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
LDMMIA Reiki Yoga S2 L3 Vod Sample Preview
LDMMIA Reiki Yoga Workshop 15 MidTerm Review
5.Universal-Franchise-and-Indias-Electoral-System.pdfppt/pdf/8th class social...
Open Quiz Monsoon Mind Game Prelims.pptx
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
Software Engineering BSC DS UNIT 1 .pptx
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Strengthening open access through collaboration: building connections with OP...
Module 3: Health Systems Tutorial Slides S2 2025
Landforms and landscapes data surprise preview
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
Ad

Loops Do While Arduino Programming Robotics

  • 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.
  • 5. Other step expressions Countdown for(i = 20; i >= 0; i--) {Serial.print(i); } Count by 10s for(i = 10; i <= 300; i=i+10) {Serial.print(i); }
  • 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);