SlideShare a Scribd company logo
1 of 20
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
C H A P T E R 4
Repetition
Structures
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Topics
Introduction to Repetition Structures
The while Loop: a Condition-
Controlled Loop
The for Loop: a Count-Controlled Loop
Calculating a Running Total
Sentinels
Input Validation Loops
Nested Loops
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Introduction to Repetition
Structures
Often have to write code that performs
the same task multiple times
Disadvantages to duplicating code
Makes program large
Time consuming
May need to be corrected in many places
Repetition structure: makes computer
repeat included code as necessary
Includes condition-controlled loops and count-
controlled loops
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
The while Loop: a Condition-
Controlled Loop
while loop: while condition is true, do
something
Two parts:
Condition tested for true or false value
Statements repeated as long as condition is true
In flow chart, line goes back to previous part
General format:
while condition:
statements
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
The while Loop: a Condition-
Controlled Loop (cont’d.)
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
The while Loop: a Condition-
Controlled Loop (cont’d.)
In order for a loop to stop executing,
something has to happen inside the
loop to make the condition false
Iteration: one execution of the body of
a loop
while loop is known as a pretest loop
– Tests condition before performing an iteration
Will never execute if condition is false to start with
Requires performing some steps prior to the loop
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Infinite Loops
Loops must contain within themselves
a way to terminate
Something inside a while loop must
eventually make the condition false
Infinite loop: loop that does not have a
way of stopping
Repeats until program is interrupted
Occurs when programmer forgets to include
stopping code in the loop
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
The for Loop: a Count-
Controlled Loop
Count-Controlled loop: iterates a
specific number of times
Use a for statement to write count-controlled
loop
Designed to work with sequence of data items
– Iterates once for each item in the sequence
General format:
for variable in [val1, val2, etc]:
statements
Target variable: the variable which is the target of
the assignment at the beginning of each iteration
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Using the range Function
with the for Loop
The range function simplifies the
process of writing a for loop
range returns an iterable object
Iterable: contains a sequence of values that can be
iterated over
range characteristics:
One argument: used as ending limit
Two arguments: starting value and ending
limit
Three arguments: third argument is step value
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
The Augmented Assignment
Operators
In many assignment statements, the
variable on the left side of the =
operator also appears on the right side
of the = operator
Augmented assignment operators:
special set of operators designed for
this type of job
Shorthand operators
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
The Augmented Assignment
Operators (cont’d.)
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Sentinels
Sentinel: special value that marks the
end of a sequence of items
When program reaches a sentinel, it knows
that the end of the sequence of items was
reached, and the loop terminates
Example: when reading an input file, empty
line can be used as a sentinel
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Input Validation Loops
Computer cannot tell the difference
between good data and bad data
If user provides bad input, program will
produce bad output
GIGO: garbage in, garbage out
It is important to design program such that
bad input is never accepted
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Input Validation Loops
(cont’d.)
Input validation: inspecting input
before it is processed by the program
If input is invalid, prompt user to enter correct
data
Commonly accomplished using a while loop
which repeats as long as the input is bad
If input is bad, display error message and receive
another set of data
If input is good, continue to process the input
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Input Validation Loops
(cont’d.)
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Nested Loops
Nested loop: loop that is contained
inside another loop
Example: analog clock works like a nested
loop
Hours hand moves once for every twelve
movements of the minutes hand: for each iteration
of the “hours,” do twelve iterations of “minutes”
Seconds hand moves 60 times for each movement
of the minutes hand: for each iteration of “minutes,”
do 60 iterations of “seconds”
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Summary
This chapter covered:
Repetition structures, including:
Condition-controlled loops
Count-controlled loops
Nested loops
Infinite loops and how they can be avoided
range function as used in for loops
Calculating a running total and augmented
assignment operators
Use of sentinels to terminate loops

More Related Content

Similar to Gaddis Python 3e Chapter 04 PPT.ppt

1.4 conditions and loops
1.4   conditions and loops1.4   conditions and loops
1.4 conditions and loops
allenbailey
 
Cso gaddis java_chapter4
Cso gaddis java_chapter4Cso gaddis java_chapter4
Cso gaddis java_chapter4
mlrbrown
 
Macasu, gerrell c.
Macasu, gerrell c.Macasu, gerrell c.
Macasu, gerrell c.
gerrell
 

Similar to Gaddis Python 3e Chapter 04 PPT.ppt (20)

My final requirement
My final requirementMy final requirement
My final requirement
 
Gaddis Python 3e Chapter 02 PPT (1).ppt
Gaddis Python 3e Chapter 02 PPT (1).pptGaddis Python 3e Chapter 02 PPT (1).ppt
Gaddis Python 3e Chapter 02 PPT (1).ppt
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Switch case and looping jam
Switch case and looping jamSwitch case and looping jam
Switch case and looping jam
 
While and For Loops
While and For LoopsWhile and For Loops
While and For Loops
 
Intro To C++ - Class 12 - For, do … While
Intro To C++ - Class 12 - For, do … WhileIntro To C++ - Class 12 - For, do … While
Intro To C++ - Class 12 - For, do … While
 
Introduction To Programming (2009 2010)
Introduction To Programming (2009 2010)Introduction To Programming (2009 2010)
Introduction To Programming (2009 2010)
 
Introduction to programming by MUFIX Commnity
Introduction to programming by MUFIX CommnityIntroduction to programming by MUFIX Commnity
Introduction to programming by MUFIX Commnity
 
1.4 conditions and loops
1.4   conditions and loops1.4   conditions and loops
1.4 conditions and loops
 
Advanced Programming Lecture 5 Fall 2016
Advanced Programming Lecture 5 Fall 2016Advanced Programming Lecture 5 Fall 2016
Advanced Programming Lecture 5 Fall 2016
 
Ite express labs
Ite express labsIte express labs
Ite express labs
 
C++ question and answers
C++ question and answersC++ question and answers
C++ question and answers
 
Cso gaddis java_chapter4
Cso gaddis java_chapter4Cso gaddis java_chapter4
Cso gaddis java_chapter4
 
Discover the real user experience with Alyvix - Icinga Camp Milan 2019
Discover the real user experience with Alyvix - Icinga Camp Milan 2019Discover the real user experience with Alyvix - Icinga Camp Milan 2019
Discover the real user experience with Alyvix - Icinga Camp Milan 2019
 
OpenControl Overview - Joshua McKenty
OpenControl Overview - Joshua McKentyOpenControl Overview - Joshua McKenty
OpenControl Overview - Joshua McKenty
 
JAN CARL BRIONES-Writing Programs Using Loops.pptx
JAN CARL BRIONES-Writing Programs Using Loops.pptxJAN CARL BRIONES-Writing Programs Using Loops.pptx
JAN CARL BRIONES-Writing Programs Using Loops.pptx
 
MPI - 2
MPI - 2MPI - 2
MPI - 2
 
Macasu, gerrell c.
Macasu, gerrell c.Macasu, gerrell c.
Macasu, gerrell c.
 
Help with Pyhon Programming Homework
Help with Pyhon Programming HomeworkHelp with Pyhon Programming Homework
Help with Pyhon Programming Homework
 
Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 

Gaddis Python 3e Chapter 04 PPT.ppt

  • 1. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Repetition Structures
  • 2. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop Calculating a Running Total Sentinels Input Validation Loops Nested Loops
  • 3. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Repetition Structures Often have to write code that performs the same task multiple times Disadvantages to duplicating code Makes program large Time consuming May need to be corrected in many places Repetition structure: makes computer repeat included code as necessary Includes condition-controlled loops and count- controlled loops
  • 4. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The while Loop: a Condition- Controlled Loop while loop: while condition is true, do something Two parts: Condition tested for true or false value Statements repeated as long as condition is true In flow chart, line goes back to previous part General format: while condition: statements
  • 5. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The while Loop: a Condition- Controlled Loop (cont’d.)
  • 6. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The while Loop: a Condition- Controlled Loop (cont’d.) In order for a loop to stop executing, something has to happen inside the loop to make the condition false Iteration: one execution of the body of a loop while loop is known as a pretest loop – Tests condition before performing an iteration Will never execute if condition is false to start with Requires performing some steps prior to the loop
  • 7. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
  • 8. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Infinite Loops Loops must contain within themselves a way to terminate Something inside a while loop must eventually make the condition false Infinite loop: loop that does not have a way of stopping Repeats until program is interrupted Occurs when programmer forgets to include stopping code in the loop
  • 9. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The for Loop: a Count- Controlled Loop Count-Controlled loop: iterates a specific number of times Use a for statement to write count-controlled loop Designed to work with sequence of data items – Iterates once for each item in the sequence General format: for variable in [val1, val2, etc]: statements Target variable: the variable which is the target of the assignment at the beginning of each iteration
  • 10. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
  • 11. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Using the range Function with the for Loop The range function simplifies the process of writing a for loop range returns an iterable object Iterable: contains a sequence of values that can be iterated over range characteristics: One argument: used as ending limit Two arguments: starting value and ending limit Three arguments: third argument is step value
  • 12. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Augmented Assignment Operators In many assignment statements, the variable on the left side of the = operator also appears on the right side of the = operator Augmented assignment operators: special set of operators designed for this type of job Shorthand operators
  • 13. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Augmented Assignment Operators (cont’d.)
  • 14. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Sentinels Sentinel: special value that marks the end of a sequence of items When program reaches a sentinel, it knows that the end of the sequence of items was reached, and the loop terminates Example: when reading an input file, empty line can be used as a sentinel
  • 15. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Input Validation Loops Computer cannot tell the difference between good data and bad data If user provides bad input, program will produce bad output GIGO: garbage in, garbage out It is important to design program such that bad input is never accepted
  • 16. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Input Validation Loops (cont’d.) Input validation: inspecting input before it is processed by the program If input is invalid, prompt user to enter correct data Commonly accomplished using a while loop which repeats as long as the input is bad If input is bad, display error message and receive another set of data If input is good, continue to process the input
  • 17. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Input Validation Loops (cont’d.)
  • 18. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Nested Loops Nested loop: loop that is contained inside another loop Example: analog clock works like a nested loop Hours hand moves once for every twelve movements of the minutes hand: for each iteration of the “hours,” do twelve iterations of “minutes” Seconds hand moves 60 times for each movement of the minutes hand: for each iteration of “minutes,” do 60 iterations of “seconds”
  • 19. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
  • 20. Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Summary This chapter covered: Repetition structures, including: Condition-controlled loops Count-controlled loops Nested loops Infinite loops and how they can be avoided range function as used in for loops Calculating a running total and augmented assignment operators Use of sentinels to terminate loops