SlideShare a Scribd company logo
1 of 17
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

Similar to Loops Do While Arduino Programming Robotics

Similar to Loops Do While Arduino Programming Robotics (20)

Loops c++
Loops c++Loops c++
Loops c++
 
Loops in c++
Loops in c++Loops in c++
Loops in c++
 
Chapter 5 java
Chapter 5 javaChapter 5 java
Chapter 5 java
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
 
Notes2
Notes2Notes2
Notes2
 
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRLoops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion Statements
 
M C6java6
M C6java6M C6java6
M C6java6
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
Looping in c language
Looping in c languageLooping in c language
Looping in c language
 
Python_Module_2.pdf
Python_Module_2.pdfPython_Module_2.pdf
Python_Module_2.pdf
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Loops
LoopsLoops
Loops
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 
Repetition, Basic loop structures, Loop programming techniques
Repetition, Basic loop structures, Loop programming techniquesRepetition, Basic loop structures, Loop programming techniques
Repetition, Basic loop structures, Loop programming techniques
 
Introduction to loops cpu
Introduction to loops  cpuIntroduction to loops  cpu
Introduction to loops cpu
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
 

Recently uploaded

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 

Recently uploaded (20)

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 

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);