Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.

Slides:



Advertisements
Similar presentations
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
Advertisements

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CS0004: Introduction to Programming Repetition – Do Loops.
Repeating Actions While and For Loops
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
11.3 Function Prototypes A Function Prototype contains the function’s return type, name and parameter list Writing the function prototype is “declaring”
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11 Object, Object- Relational, and XML: Concepts, Models, Languages,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Part 1 Conditionals and Loops.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Chapter 2 Control. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Repetition, quick overview.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
CS 100 Introduction to Computing Seminar October 7, 2015.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Programming with Loops. When to Use a Loop  Whenever you have a repeated set of actions, you should consider using a loop.  For example, if you have.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
1 Looping Chapter 6 2 Getting Looped in C++ Using flags to control a while statement Trapping for valid input Ending a loop with End Of File condition.
Topics Introduction to Repetition Structures
Chapter 5: Repetition Structures
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Topics Introduction to Repetition Structures
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Augmented Assignment Operators Sentinels Input Validation Loops
Alternate Version of STARTING OUT WITH C++ 4th Edition
Using the Target Variable Inside the Loop
Chapter 7: Input Validation
Topics Introduction to Repetition Structures
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition Structures

1-2 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Repetition Structures Concept: The repetition structure causes a statement or set of statements to execute repeatedly.

1-3 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Repetition Structures Condition-Controlled and Count-Controlled Loops A condition-controlled loop uses a true/false condition to control the number of times that it repeats. A count-controlled loop repeats a specific number of times.

1-4 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The while Loop: a Condition-Controlled Loop Concept: A condition-controlled loop causes a statement or set of statements to repeat as long as a condition is true. In Python you use the while statement to write a condition-controlled loop.

1-5 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The while Loop: a Condition-Controlled Loop In Python … while condition: statement etc. Figure 5-1 The logic of a while loop

1-6 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The while Loop: a Condition-Controlled Loop Figure 5-2 The while loop

1-7 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The while Loop: a Condition-Controlled Loop The while Loop is a Pretest Loop, which means it tests its condition before performing an iteration.

1-8 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The for Loop: a Count-Controlled Loop Concept: A count-controlled loop iterates a specific number of times. In Python you use the for statement to write a count-controlled loop.

1-9 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The for Loop: a Count-Controlled Loop In Python … for variable in [value1, value2, etc.]: statement etc.

1-10 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The for Loop: a Count-Controlled Loop Using the range Function with the for Loop for num in [0, 1, 2, 3, 4]: print num for num in range (5): print num

1-11 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The for Loop: a Count-Controlled Loop Using the range Function with the for Loop First argument, 1, is the starting value for the list Second argument, 10, is the ending limit of the list Third argument, 2, is the step value for num in range (1, 10, 2): print num

1-12 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The for Loop: a Count-Controlled Loop Program 5-12 (user_squares2.py)

1-13 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The for Loop: a Count-Controlled Loop Generating Lists that Range from Highest to Lowest for num in range (5, 0, -1): print num

1-14 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Calculating a Running Total Concept: A running total is a sum of numbers that accumulates with each iteration of a loop. The variable used to keep the running total is called an accumulator.

1-15 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Calculating a Running Total Programs that calculate the total of a series of numbers typically use two elements: A loop that reads each number in the series A variable that accumulates the total of the numbers as they are read.

1-16 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Calculating a Running Total Figure 5-7 Logic for calculating a running total

1-17 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Calculating a Running Total The Augmented Assignment Operators Table 5-2 The age variable references the value 25

1-18 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Sentinels Concept: A sentinel is a special value that marks the end of a sequence of values.

1-19 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Input Validation Loops Concept: Input validation is the process of inspecting data that has been input to a program, to make sure it is valid before it is used in a computation. Input validation is commonly done with a loop that iterates as long as an input variable references bad data.

1-20 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Input Validation Loops Error Trap or Error Handler Figure 5-8 Logic containing an input validation loop

1-21 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Input Validation Loops Priming Read # Get a test score. Score = input(‘Enter a test score: ‘) # Make sure it is not less than 0. while score < 0: print ‘ERROR: The score cannot be negative.’ score = input (‘Enter the correct score: ‘)

1-22 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Nested Loops Concept: A loop that is inside another loop is called a nested loop.

1-23 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Nested Loops About Nested Loops: The inner loop goes through all of its iterations for every single iteration of the outer loop Inner loops complete their iterations faster than outer loops for hours in range(24): for minutes in range(60): for seconds in range(60): print hours, ‘:’, minutes, ‘:’, seconds

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley QUESTIONS ? Chapter 5 Repetition Structures