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

overview questions
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

1

1

Aaron Stevens

30 March 2009

CS101 Lecture 25: Python: Repetition

Patterns: Counter-Controlled, Interactive, and Sentinel

2

Overview/Questions

– Review: Flow of Control – The idea of iteration: how to conditionally repeat statements within a programs. – Strategies for repetition.

slide-2
SLIDE 2

2

3

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.

4

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.

slide-3
SLIDE 3

3

5

Review Logical Expressions

Logical Expression Programming statement which achieves a Boolean logical result (True or False). Example: using if-elif-else statements

6

Repetition in Action

Everybody’s favorite drinking song…

slide-4
SLIDE 4

4

7

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>

8

Patterns for Indefinite Loops

Several common patterns:

 Counter-controlled Loop  Interactive Loop  Sentinel-controlled Loop

slide-5
SLIDE 5

5

9

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

10

Counter-Controlled Pattern

Example, revisited: Discuss: initialize counter, logical expression, counter update.

slide-6
SLIDE 6

6

11

Counter-Controlled Pattern

Example: Calculating factorial

12

Counter-Controlled Pattern

Example: Calculating an Average

slide-7
SLIDE 7

7

13

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

14

Interactive Loop Pattern

Example: averaging quiz grades

– indefinite loop version, interactive pattern.

slide-8
SLIDE 8

8

15

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

16

Sentinel Loop Pattern

Example: averaging quiz grades

– indefinite loop version, sentinel pattern

slide-9
SLIDE 9

9

17

Take-Away Points

– Indefinite Loops – Counter-controlled loops – Interactive loops – Sentinel-controlled loops

18

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)