+ All Categories
Home > Documents > CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while...

CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while...

Date post: 20-May-2020
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
23
While Loops CSCI 1101B
Transcript
Page 1: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

While Loops

CSCI 1101B

Page 2: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

Today’s Outline

● Iteration (review)

● When Should We Not Use For-Each Loops?

● While Loops

● Controlling Loops○ The Break Statement○ The Continue Statement

Page 3: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

Example - First, Recall The Turtle Module

from turtle import *

joe = Turtle()joe.shape("turtle")

Page 4: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

Example: Running a Race

joe.forward(100) # The 100 meter dash!done()

# ...but what if we wanted to take # things one step at a time?

# (e.g., perhaps there are # obstacles to look out for!)

Page 5: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

Example: Running a Race

joe.forward(1)joe.forward(1)joe.forward(1)joe.forward(1)

# and so on…!

done()

Page 6: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

Example: Running a Race

joe.forward(1)joe.forward(1)joe.forward(1)joe.forward(1)

# and so on…!

done()

# How could we make this better?

Page 7: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

Example: Running a Race (For-Each Loop)

num_iterations = 4

for i in range(num_iterations): joe.forward(1)

done()

Page 8: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

Example: Running a Race (For-Each Loop)

num_iterations = 4

for i in range(num_iterations): joe.forward(1)

done()

# Let’s learn another way...

Page 9: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

Example: Running a Race (While Loop)

num_iterations = 4current_num = 0

while current_num < num_iterations: joe.forward(1) current_num += 1

done()

Page 10: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

Example: Running a Race (While Loop)

num_iterations = 4current_num = 0 # initialization

while current_num < num_iterations: # test condition joe.forward(1) current_num += 1 # update

done()

Page 11: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

Example: Running a Race

Why have while loops if for-each loops work just fine?

Page 12: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

Example: Running a Race

Why have while loops if for-each loops work just fine?

What if we didn’t know the number of iterations?

What if we just had a stopping point that would happen at some point along the way?

Page 13: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

While Loops

When we don’t know how long a loop will last, we can use a while loop.

while <<condition>>: <<block>>

Page 14: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

Example: Running a Race (While Loop)

from random import randint # Lets us get a random int!

simon_says = True

while simon_says: joe.forward(1) if not randint(0, 2): # What’s this mean? simon_says = Falsedone()

Page 15: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

Summary: When are For Loops not the best?

What if we don’t know how long a loop should last?

○ “Stir until the mixture is boiling”

○ “Keep the game going until the user quits”

○ “Keep searching for a new answer until you find the right one”

Page 16: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

While Loops: Example 2

text = “”

while text != “quit”: text = input(“Enter command or quit: ”)

if text == “quit”: print(“...exiting program.”) elif text == “sing”: print(“La la la, la li da.”) else: print(“Come again?”)

Page 17: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

Caution: Infinite Loops

If your test condition never evaluates to False, the instructions will endlessly loop.

while True: print(“This is the song that never ends…”)

Make sure you have a feasible terminating condition to avoid these infinite loops.

(Remember, you can restart the shell if this happens accidentally!) with apologies to Norman Martin

Page 18: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

Controlling Loops

Python lets us control iteration in two main ways:

1. We can break out of a loop using the break keyword

2. We can skip ahead to the next iteration with the continue keyword

Page 19: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

The Break Statement

text = “”

while True: text = input(“Enter command or quit: ”)

if text == “quit”: print(“...exiting program.”) break if text == “sing”: print(“La la la, la li da.”) print(“Hooray!”)

Page 20: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

The Continue Statement

for num in range(10):

if num % 2 == 1:

continue

print(num, end=” ”)

Page 21: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

The Continue Statement

for num in range(10):

if num % 2 == 1:

continue

print(num, end=” ”)

This code prints out: 0 2 4 6 8

Page 22: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

Good Practice: Don’t use Break/Continue much

What happens when we depend on breaks and continues?

● Loop termination is not clearly explicit

● Encourages lazy programming

● Makes it harder to keep code updated

Page 23: CSCI 1101B While Loops - Bowdoin Collegesharmon/static/1101/lecs/1101_lec11.pdf · Why have while loops if for-each loops work just fine? What if we didn’t know the number of iterations?

Summary

When do we use the following?

● For each and while loops

● Break and continue statements

Be careful of infinite loops!


Recommended