Top Banner
Repetition Structures cause statement(s) to execute repeatedly.
39

Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Apr 01, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Repetition Structures

cause statement(s) to execute repeatedly.

Page 2: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Agenda1. The while loop - a condition-controlled loop2. The for loop - a count-controlled loop

3. Infinite loops

4. The range() function is often used with for loops.

5. The Augmented Assignment Operators: += -= *= /= %=

Page 3: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

The while loop is a condition-controlled loop

WHILE a condition is true,

execute conditional code.

Page 4: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

The while loop exampleThis condition is tested.

If the condition is true, these statements are executed, and then the loop starts over.

Page 5: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Program output

Page 6: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

AssignmentCould you please re-write the while loop, so that the count is only printed if it’s even:

Page 7: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Solution 1

Page 8: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Solution 2

Page 9: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Solution 3 w augmented assignment operator ‘+=’

Page 10: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Infinite Loop

CTRL+C to interrupt.

Page 11: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Infinite Loop Example

Page 12: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

What will this program print?

Page 13: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Program Output

Page 14: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

The augmented assignment operator ‘%=’

Page 15: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

The for loopThe for statement is designed to work with a sequence of data items. Statement(s) execute once for each item in the sequence.

Page 16: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

The for loop

Page 17: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

the for loop with strings

Page 18: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

AssignmentCould you please write a for loop that displays odd numbers 1 through 9.

Page 19: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Solution

Page 20: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

The range() function is often used with for loops.

Page 21: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

range(stop) range(start, stop [, step])

Page 22: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Programming Exercise

Page 23: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Desired program output

Page 24: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Solution

Page 25: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Letting the user control

the number of loop iterations.

Page 26: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Desired program output.

User Input

Page 27: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …
Page 28: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Calculating a running total.

Page 29: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Program output.

Page 30: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Solution

Page 31: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Sentinels

Page 32: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

When a program reads the sentinel value, the loop terminates.

Page 33: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Program output.Using a negative number to terminate a loop...

Page 34: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Program

Page 35: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Input Validation Loops

Page 36: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Writing an Input Validation Loop

Retail price = wholesale cost * 2.5

Wholesale cost cannot be negative.

Page 37: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …
Page 38: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Nested Loops

Page 39: Repetition Structures - GramercyDatadata items. Statement(s) execute once for each item in the sequence. The for loop the for loop with strings Assignment Could you please …

Nested loop example