[go: up one dir, main page]

0% found this document useful (0 votes)
4 views21 pages

CMPUT 174 - While Loops

Uploaded by

chengmichaelj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views21 pages

CMPUT 174 - While Loops

Uploaded by

chengmichaelj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

CMPUT 174

While Loops

June 20 2022 1
Lecture Outline
❏ while Statements

❏ for Loops vs. while Loops

❏ Infinite Loops

June 20 2022 2
What is a while Statement?
● while statements are another type of
compound repetition statement that can be
used to evaluate code repeatedly

The expression in the


Header header needs to evaluate
to either true or false
while <expression>:
<statement suite>
The statement suite will
continually be evaluated
Suite or while the expression is true
Loop Body
It will not be evaluated
when the expression is false

June 20 2022 3
while Statements
● A code example of a while statement using the
user’s input

>>> word = ''


>>> while len(word) < 8:
... word = input("Enter word: ")
...
Enter word: hello
Enter word: candy
Enter word: pen The entered word textbook is
Enter word: textbook 8 characters – when the
program loops back, the
header will evaluate to False;
terminating the while loop

June 20 2022 4
for Statements vs. while Statements

For Loops While Loops

● Definitive iterations – ● Indefinite iterations –


loop in which the a loop that will
number of times it is ● Compound continue to run an
going to execute is statements infinite number of
known in advance times until a condition
● Evaluates
is satisfied
code
● Used to loop a specific
repeatedly
number of times; ● Used to repeatedly
evaluate the
○ Ex. for every
statement suites until
element in a list,
a specific condition
string, or tuple
is reached

June 20 2022 5
for Statements vs. while Statements
● In some cases, we know exactly how many
times a suite will be repeated. In such cases we
use a for loop
● For example, if want to make a 3x3 Tic Tac Toe
board then we know that we have to make
exactly 9 squares

● We’ll look at
some examples
of using a for
loop

June 20 2022 6
for Statements vs. while Statements
● In some situations we do not know exactly how
many times a suite should be repeated
● For example, while playing a game of checkers,
each player makes a move. The moves are
made until one player wins. We don't know how
many moves each player will make.

● We’ll look at
some examples
of indefinite
repetition

June 20 2022 7
Infinite Loops
● When using while statements, be careful of
infinite loops
● Infinite loops happen when the expression in
the header of the while statement never
evaluates to False

★ Consequently the suite of the while statement will


continuously be evaluated without any way to stop!

June 20 2022 8
Infinite Loops
● Example of an infinite loop!

>>> clone = True


>>> while clone == True:
... print("Make it double!")
...
Make it double!
It really
Make it double!
doesn’t stop…
Make it double!
Make it double!
etc...

June 20 2022 9
Infinite Loops
● The main idea to prevent an infinite loop is to
ensure there is some way to make a change
within the suite of the while statement
● The expression in the header of the while
statement must evaluate to False at some
point in time

June 20 2022 10
Infinite Loops
● Changing the code so that it’s no longer an infinite loop!

>>> clone = True >>> clone = True


times = 5
0
1
2
3
4
>>> while clone == True: >>> times = 5
... print("Make it double!") >>> while clone == True:
... ... print("Make it double!")
Make it double! ... times = times - 1
Make it double! ... if times <= 0:
Make it double!
... clone = False
Make it double!
...
etc...
Make it double!
Make it double!
Make it double!
Make it double!
Make it double!

June 20 2022 11
Reminder
● Online Activities:
○ Assigned Readings:
■ Module 4

○ Complete Quiz 1

June 20 2022 12
Sookie’s Bistro

Image Source: https://giphy.com/gifs/gilmore-girls-melissa-mccarthy-sookie-st-james-l1J9zTcxkTOpjlwgU

June 20 2022 13
Practice Problem 1!
Trout Cakes or Seafood Medley-while loop
'''Sookie wants to make trout cakes for her
Bistro.

The recipe calls for 5 trouts. In order to catch


fresh trout, she goes fishing in a nearby lake.

Sookie wants to keep the trouts that she catches


and release any other fish back into the lake.
However, she must abide by the local fishery
regulations.

June 20 2022 14
Practice Problem 1!

The fishery regulation in her town has set


a daily catch limit of 7 which means that
Sookie cannot have in her possession more than
7 fish in one day.

Since mortality rate of fish that are caught


and released is high, there is also a daily
catch and release limit of 3 which means that
Sookie can catch and immediately release not
more than 3 fish in one day.
June 20 2022 15
Practice Problem 1!

The catch and release limit prevents Sookie


from releasing all fish that are not trouts.

Sookie wants to stop fishing once she has


enough fish for the trout cakes.

If she is unable to get the required number of


trouts and has reached the daily catch limit,
she will make a seafood medley from her catch
instead.

June 20 2022 16
Practice Problem 1!
Create a program that helps Sookie
keep track of how many trouts she has caught
without exceeding any limits.

The program prints 'Trout cakes' if she has


enough trouts otherwise prints 'Seafood
medley'.
'''

June 20 2022 Image Source: http://clipart-library.com/ 17


Practice Problem 2!
while loop
'''In order to promote her bistro, Sookie would like
to run a Jelly Bean Guessing Game for her customers.

She informs the customer that the number of jelly


beans could be anywhere in the range of 2000 to 5000.

June 20 2022 18
Practice Problem 2!
Each customer gets 3 attempts to guess the
number of jelly beans in the jar.

If the customer gets it right in their first


attempt,they receive a 50% off coupon that they can
redeem at their next order.

If the first attempt fails, the customer is given the


sum of all digits of the number of jelly beans as a
hint.

June 20 2022 19
Practice Problem 2!
If the customer is able to guess correctly in the second
attempt they get a 25% off coupon that they can redeem
at their next order.

If the second attempt fails, the customer is given the


first and last digit of the number of jelly beans as a
second and final hint.

June 20 2022 20
Practice Problem 2!
If the customer is able to guess correctly in the
third attempt they get a 10% off coupon that they can
redeem at their next order.

Create a program that asks for the number of jelly beans


and displays how much discount the customer can get in
their next order.
'''

June 20 2022 Image Source: http://clipart-library.com/ 21

You might also like