overview questions
play

Overview/Questions Review: Flow of Control The idea of iteration: - PDF document

CS101 Lecture 25: Python: Repetition Patterns: Counter-Controlled, Interactive, and Sentinel Aaron Stevens 30 March 2009 1 Overview/Questions Review: Flow of Control The idea of iteration: how to conditionally repeat statements


  1. CS101 Lecture 25: Python: Repetition Patterns: Counter-Controlled, Interactive, and Sentinel Aaron Stevens 30 March 2009 1 Overview/Questions – Review: Flow of Control – The idea of iteration: how to conditionally repeat statements within a programs. – Strategies for repetition. 2 1

  2. Flow of Control Sequential Execution Each instruction is executed in order they are written (after the previous one, before the next on). Functions Enable procedural decomposition. Repeat statements by calling functions multiple times. 3 Flow of Control Selection Some statements are executed while others are not. Repetition Statements can be repeated some fixed number of time, or else can be repeated until some event signals they should not be repeated any more. 4 2

  3. Review Logical Expressions Logical Expression Programming statement which achieves a Boolean logical result (True or False). Example: using if-elif-else statements 5 Repetition in Action Everybody’s favorite drinking song… 6 3

  4. The Indefinite Loop The indefinite loop is controlled by a conditional expression. while statement A structure which evaluates a logical expression, and repeats a controlled block of statements. while <condition>: <body> 7 Patterns for Indefinite Loops Several common patterns:  Counter-controlled Loop  Interactive Loop  Sentinel-controlled Loop 8 4

  5. Counter-Controlled Pattern Counter-Controlled Loop Evaluates a counter in loop’s logical expression: Pseudo-code for this pattern: initialize counter while counter > critical-value do controlled block statements decrement (increment) counter 9 Counter-Controlled Pattern Example, revisited: Discuss: initialize counter, logical expression, counter update. 10 5

  6. Counter-Controlled Pattern Example: Calculating factorial 11 Counter-Controlled Pattern Example: Calculating an Average 12 6

  7. Interactive Loop Pattern Interactive Loop Prompts user whether or not to continue. Pseudo-code for this pattern: set moredata to “yes” while moredata is “yes” get next data item process data item ask user if there is moredata 13 Interactive Loop Pattern Example: averaging quiz grades – indefinite loop version, interactive pattern. 14 7

  8. Sentinel Loop Pattern Sentinel Loop Checks input for a special value to determine whether or not to continue. get first data item while data item is not the sentinel process data item get next data item 15 Sentinel Loop Pattern Example: averaging quiz grades – indefinite loop version, sentinel pattern 16 8

  9. Take-Away Points – Indefinite Loops – Counter-controlled loops – Interactive loops – Sentinel-controlled loops 17 Student To Dos – HW10 due Tuesday 3/31 – Reading: How to Think Like A Computer Scientist: Learning with Python Available online at: http://openbookproject.net//thinkCSpy/  Ch06 (Monday/Wednesday)  Ch09, 9.1-9.7, 9.13 (Friday) 18 9

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend