SlideShare a Scribd company logo
1 of 31
Repetition (Looping Concept)
After studying this chapter you will be able to: O BJECTIVES  Understand the basic components of a loop: initialization, control expression, and update.  Understand and use pretest, post-test, and count-controlled loops.  Differentiate between event-controlled and counter-controlled loops.  Write loops in C++ using  while ,  for , and  do...while  loops.  Understand the limitations and use of  break  and  continue  statements in loops.
Concept of a Loop
[object Object],[object Object],[object Object]
Figure : The concept of a loop
Pretest and Post-Test Loops
[object Object],[object Object],[object Object],[object Object]
Pretest Loop In each iteration, the loop control expression is tested first. If it is true, the loop action(s) is executed; if it is false, the loop is terminated. Post-test Loop In each iteration, the loop action(s) are executed. Next, the loop control expression is tested. If it is true, a new iteration is started; otherwise, the loop terminates. Note:
Figure : Pretest and post-test loops “ WHILE” test at the top….  “WHILE” test at the bottom CONDITION CONDITION
Figure : Two different strategies for starting exercise CONDITION CONDITION False True
Figure :  Minimum number of iterations in two loops It is important that you understand this critical difference between the two basic types…
Initialization and Updating
Figure : Loop initialization and updating The  initialization  may optionally be part of the loop structure.
Figure : Initialization and updating for exercise
Event-Controlled and Counter-Controlled  Loops
Figure : Event-controlled loop concept
Figure : Counter-controlled loop concept
Loops in C++
Figure : C++ loop constructs ,[object Object],[object Object]
Figure : The while statement while  the expression is true, do …..
Figure : Compound  while  statement
Figure : for  statement The  for  is just a special case of a  while  loop.
The  for  loop is constructed in this way… for (  initialization statement(s) , while test expression ,   update statement(s)  )‏ { body of loop… }
A  for  loop is used when your loop is to be executed a known number of times. You can do the same thing with a  while  loop, but the  for  loop is easier to read and more natural for counting loops. Note:
Figure : Compound  for  statement
Figure : Comparing  for  and  while  loops
In C/C++ the  for  loop counter is almost always initialized to a  ZERO .  So, if you want to do something 5 times, the  for  loop would be coded like this… for (int i = 0; i < 5; i++)‏ { do something… } It is also legal to have the test expression limit be a variable, as in: for (int i = 0; i < Num; i++)‏
Figure :  Format of the  do…while  statement
Pre-test and post-test loops
Assignment ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The End

More Related Content

What's hot (20)

Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops in c++ programming language
 
The Loops
The LoopsThe Loops
The Loops
 
Python unit 2 as per Anna university syllabus
Python unit 2 as per Anna university syllabusPython unit 2 as per Anna university syllabus
Python unit 2 as per Anna university syllabus
 
While loop
While loopWhile loop
While loop
 
Control statements
Control statementsControl statements
Control statements
 
Loops c++
Loops c++Loops c++
Loops c++
 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in C
 
Arrays and structures
Arrays and structuresArrays and structures
Arrays and structures
 
For Loop
For LoopFor Loop
For Loop
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
Control structures repetition
Control structures   repetitionControl structures   repetition
Control structures repetition
 
Nested loops
Nested loopsNested loops
Nested loops
 
C++ goto statement tutorialspoint
C++ goto statement   tutorialspointC++ goto statement   tutorialspoint
C++ goto statement tutorialspoint
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Loop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationLoop(for, while, do while) condition Presentation
Loop(for, while, do while) condition Presentation
 
Linked list
Linked listLinked list
Linked list
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Algorithmic Notations
Algorithmic NotationsAlgorithmic Notations
Algorithmic Notations
 
Recursion in c++
Recursion in c++Recursion in c++
Recursion in c++
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 

Viewers also liked

Viewers also liked (20)

Loops in C Programming
Loops in C ProgrammingLoops in C Programming
Loops in C Programming
 
Loops
LoopsLoops
Loops
 
C++ loop
C++ loop C++ loop
C++ loop
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
 
Loops
LoopsLoops
Loops
 
Looping and switch cases
Looping and switch casesLooping and switch cases
Looping and switch cases
 
Do While and While Loop
Do While and While LoopDo While and While Loop
Do While and While Loop
 
Basics of C programming
Basics of C programmingBasics of C programming
Basics of C programming
 
Nesting of for loops using C++
Nesting of for loops using C++Nesting of for loops using C++
Nesting of for loops using C++
 
Chapter 05 looping
Chapter 05   loopingChapter 05   looping
Chapter 05 looping
 
Do...while loop structure
Do...while loop structureDo...while loop structure
Do...while loop structure
 
Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C Programming
 
7 problem solving with loops
7 problem solving with loops7 problem solving with loops
7 problem solving with loops
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition Structure
 
C program language tutorial for loop while loop do while loop
C program language tutorial for loop while loop do while loopC program language tutorial for loop while loop do while loop
C program language tutorial for loop while loop do while loop
 
C programming slide c01
C programming slide c01C programming slide c01
C programming slide c01
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Looping in C
Looping in CLooping in C
Looping in C
 

Similar to Loops

loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docxJavvajiVenkat
 
Chapter 3 - Flow of Control Part II.pdf
Chapter 3  - Flow of Control Part II.pdfChapter 3  - Flow of Control Part II.pdf
Chapter 3 - Flow of Control Part II.pdfKirubelWondwoson1
 
Instrumentation and measurements
Instrumentation and measurementsInstrumentation and measurements
Instrumentation and measurementsTuba Tanveer
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while LoopJayBhavsar68
 
Loops in C.net.pptx
Loops in C.net.pptxLoops in C.net.pptx
Loops in C.net.pptxFajela
 
LOOPING IN C- PROGRAMMING.pptx
LOOPING IN C- PROGRAMMING.pptxLOOPING IN C- PROGRAMMING.pptx
LOOPING IN C- PROGRAMMING.pptxAFANJIPHILL
 
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopPriyom Majumder
 
Chp4_C++_Control Structures-Part2_Iteration.pptx
Chp4_C++_Control Structures-Part2_Iteration.pptxChp4_C++_Control Structures-Part2_Iteration.pptx
Chp4_C++_Control Structures-Part2_Iteration.pptxssuser10ed71
 
C++ Loops General Discussion of Loops A loop is a.docx
C++ Loops  General Discussion of Loops A loop is a.docxC++ Loops  General Discussion of Loops A loop is a.docx
C++ Loops General Discussion of Loops A loop is a.docxhumphrieskalyn
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, LoopingMURALIDHAR R
 
Cpp loop types
Cpp loop typesCpp loop types
Cpp loop typesRj Baculo
 

Similar to Loops (20)

ch5.ppt
ch5.pptch5.ppt
ch5.ppt
 
Loops in c++
Loops in c++Loops in c++
Loops in c++
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
Chapter 3 - Flow of Control Part II.pdf
Chapter 3  - Flow of Control Part II.pdfChapter 3  - Flow of Control Part II.pdf
Chapter 3 - Flow of Control Part II.pdf
 
Instrumentation and measurements
Instrumentation and measurementsInstrumentation and measurements
Instrumentation and measurements
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
 
itretion.docx
itretion.docxitretion.docx
itretion.docx
 
Loops in C.net.pptx
Loops in C.net.pptxLoops in C.net.pptx
Loops in C.net.pptx
 
LOOPING IN C- PROGRAMMING.pptx
LOOPING IN C- PROGRAMMING.pptxLOOPING IN C- PROGRAMMING.pptx
LOOPING IN C- PROGRAMMING.pptx
 
Fundamentals of Programming Chapter 7
Fundamentals of Programming Chapter 7Fundamentals of Programming Chapter 7
Fundamentals of Programming Chapter 7
 
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
 
Chp4_C++_Control Structures-Part2_Iteration.pptx
Chp4_C++_Control Structures-Part2_Iteration.pptxChp4_C++_Control Structures-Part2_Iteration.pptx
Chp4_C++_Control Structures-Part2_Iteration.pptx
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
 
Loops In C++
Loops In C++Loops In C++
Loops In C++
 
C++ Loops General Discussion of Loops A loop is a.docx
C++ Loops  General Discussion of Loops A loop is a.docxC++ Loops  General Discussion of Loops A loop is a.docx
C++ Loops General Discussion of Loops A loop is a.docx
 
Loops in C.pptx
Loops in C.pptxLoops in C.pptx
Loops in C.pptx
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
M C6java6
M C6java6M C6java6
M C6java6
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
Cpp loop types
Cpp loop typesCpp loop types
Cpp loop types
 

More from Kulachi Hansraj Model School Ashok Vihar

More from Kulachi Hansraj Model School Ashok Vihar (20)

Edge 2010
Edge 2010Edge 2010
Edge 2010
 
Story 1
Story 1Story 1
Story 1
 
Butterfly
ButterflyButterfly
Butterfly
 
A message of stephen covey
A message of stephen coveyA message of stephen covey
A message of stephen covey
 
Stack Applications
Stack ApplicationsStack Applications
Stack Applications
 
Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions
 
robotics workshop Aug.09
robotics workshop Aug.09robotics workshop Aug.09
robotics workshop Aug.09
 
Computer System Organization
Computer System OrganizationComputer System Organization
Computer System Organization
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 
Tut Constructor
Tut ConstructorTut Constructor
Tut Constructor
 
Think It
Think ItThink It
Think It
 
Edge2008
Edge2008Edge2008
Edge2008
 
Programming Methodology
Programming MethodologyProgramming Methodology
Programming Methodology
 
Arrays
ArraysArrays
Arrays
 
Robot Timeline
Robot TimelineRobot Timeline
Robot Timeline
 
Prefix Postfix
Prefix PostfixPrefix Postfix
Prefix Postfix
 
Looping
LoopingLooping
Looping
 
Computer Fundamentals
Computer FundamentalsComputer Fundamentals
Computer Fundamentals
 
Number System
Number SystemNumber System
Number System
 
Computer Parts
Computer PartsComputer Parts
Computer Parts
 

Recently uploaded

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
 
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
 
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
 
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
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
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
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
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
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 

Recently uploaded (20)

INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
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
 
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
 
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
 
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
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
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
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
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
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 

Loops

  • 2. After studying this chapter you will be able to: O BJECTIVES  Understand the basic components of a loop: initialization, control expression, and update.  Understand and use pretest, post-test, and count-controlled loops.  Differentiate between event-controlled and counter-controlled loops.  Write loops in C++ using while , for , and do...while loops.  Understand the limitations and use of break and continue statements in loops.
  • 4.
  • 5. Figure : The concept of a loop
  • 7.
  • 8. Pretest Loop In each iteration, the loop control expression is tested first. If it is true, the loop action(s) is executed; if it is false, the loop is terminated. Post-test Loop In each iteration, the loop action(s) are executed. Next, the loop control expression is tested. If it is true, a new iteration is started; otherwise, the loop terminates. Note:
  • 9. Figure : Pretest and post-test loops “ WHILE” test at the top…. “WHILE” test at the bottom CONDITION CONDITION
  • 10. Figure : Two different strategies for starting exercise CONDITION CONDITION False True
  • 11. Figure : Minimum number of iterations in two loops It is important that you understand this critical difference between the two basic types…
  • 13. Figure : Loop initialization and updating The initialization may optionally be part of the loop structure.
  • 14. Figure : Initialization and updating for exercise
  • 19.
  • 20. Figure : The while statement while the expression is true, do …..
  • 21. Figure : Compound while statement
  • 22. Figure : for statement The for is just a special case of a while loop.
  • 23. The for loop is constructed in this way… for ( initialization statement(s) , while test expression , update statement(s) )‏ { body of loop… }
  • 24. A for loop is used when your loop is to be executed a known number of times. You can do the same thing with a while loop, but the for loop is easier to read and more natural for counting loops. Note:
  • 25. Figure : Compound for statement
  • 26. Figure : Comparing for and while loops
  • 27. In C/C++ the for loop counter is almost always initialized to a ZERO . So, if you want to do something 5 times, the for loop would be coded like this… for (int i = 0; i < 5; i++)‏ { do something… } It is also legal to have the test expression limit be a variable, as in: for (int i = 0; i < Num; i++)‏
  • 28. Figure : Format of the do…while statement
  • 30.