Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Similar presentations


Presentation on theme: "Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License."— Presentation transcript:

1 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Chapter 7 Iteration

2 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Overview

3 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. for Loops

4 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Iterating on Lists

5 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Accumulation Variables

6 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Accumulation Variables

7 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Iterating on Strings

8 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Iterating on Strings

9 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Iterating on Substrings

10 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Iterating on Ranges

11 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Iterating on Ranges In the simplest case, we can use for and range to repeat a block of code a fixed number of times:

12 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Iterating on Ranges Using the start, stop and step parameters the range function can generate many linear sequences: These sequences can count down as well as up:

13 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Iterating on Ranges We can also create more complicated sequences by using the range values to parameterize a more complicated calculation. This loop calculates n factorial:

14 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Iterating on Ranges range is a general mechanism for repetition. In this example that draws a square, the variable i isn't used and that's ok.

15 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Iterating on Ranges We can generalize the last example such that it draws a polygon:

16 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Iterating on Ranges

17 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Nesting Loops

18 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Nesting Loops

19 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Nesting Loops

20 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Nesting Loops

21 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. while Loops

22 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Newton’s Method

23 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Calculating Loops

24 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Infinite Loops

25 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Event Loops

26 Copyright © 2014 Dr. James D. Palmer; http://jdpalmer.org/cs1 This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Break and Continue


Download ppt "Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License."

Similar presentations


Ads by Google