Lecture 8
While Loops
CSc 250 - Spring 2018
Benjamin Dicken
Repetition
while (condition):
line A
line B
. . .
line N
Every while-loop begins with the while keyword
Must specify a condition. this can be any boolean expression.
Syntax closely resembles that of an if-statement
Lines in the body of the while-loop must be indented 4 spaces. These lines are repeated until the condition is no longer true.
While-loop simulation
counter = 0
while (counter < 5):
print("Hello World!")
counter = counter + 1
ICA
While-loop simulation
while (True):
print("Hello World!")
ICA
While-loop simulation
counter = 0
while (counter < 5 and False):
print("Hello World!")
counter = counter + 1
ICA
While-loop simulation
tracker = 0
increment = 3
while (tracker < 15):
print("Hello World!")
tracker = tracker + increment
ICA
While-loop simulation
a = 10
b = 0
while (a != b):
print("Hello World!")
a = a - 1
b = b + 1
ICA
Common While-loop usage
index = 0
while (index < 10):
print("hello there")
index = index + 1
Common While-loop usage
index = 0
while (index < 10):
print("hello there")
index = index + 1
Loop control variable
Loop limit
Loop control variable increment
Common While-loop usage
index = 0
while (index < 10):
print("hello there")
index += 1
Nesting an if-statement
index = 0
while (index < 10):
if (index < 5):
print("less than 5")
else:
print("less than 10")
index += 1
Nesting an if-statement
index = 0
while (index < 10):
if (index < 5):
print("less than 5")
else:
print("less than 10")
index += 1
While-loop simulation
index = 0
while (index < 18):
if (index < 12)
index += 5
else:
index += 1
print("Hello World!")
ICA
While-loop simulation
index = 0
while (index < 20):
if (index < 14)
index += 4
print("Hello World!")
index += 1
ICA
Guess the name
What is my name? Joe
-- Wrong, guess again --
What is my name? Eric
-- Wrong, guess again --
What is my name? Ben
-- Correct! Bye --
ICA
Guess the name
What is my name? Joe
-- Wrong, 3 guesses left --
What is my name? Eric
-- Wrong, 2 guesses left --
What is my name? Annie
-- Wrong, 1 guesses left --
What is my name? Carol
-- Out of guesses --
What is my name? Joe
-- Wrong, 3 guesses left --
What is my name? Ben
-- Correct! Bye --
ICA
Guess the name
...
----
You FAILED!
Attempts: 4
...
----
You PASSED!
Attempts: 2
ICA
Combine loops, multiplication
*
***
*****
*******
X
X
*
***
*****
*******
*********
X
*
***
*****
*******
*********
***********
*************
X
The Solution
The Solution
Read in the control inputs
The Solution
bh = int(input("tree body height: "))
th = int(input("tree trunk height:" ))
Read in the control inputs
The Solution
bh = int(input("tree body height: "))
th = int(input("tree trunk height:" ))
Read in the control inputs
Print out the tree body
The Solution
bh = int(input("tree body height: "))
th = int(input("tree trunk height:" ))
# Print the tree
index = 0
while (index < bh):
space = bh - index - 1
row = index * 2 + 1
print(" " * space, end="")
print("^" * row)
index += 1
Read in the control inputs
Print out the tree body
The Solution
bh = int(input("tree body height: "))
th = int(input("tree trunk height:" ))
# Print the tree
index = 0
while (index < bh):
space = bh - index - 1
row = index * 2 + 1
print(" " * space, end="")
print("^" * row)
index += 1
Read in the control inputs
Print out the tree body
Print the trunk
The Solution
bh = int(input("tree body height: "))
th = int(input("tree trunk height:" ))
# Print the tree
index = 0
while (index < bh):
space = bh - index - 1
row = index * 2 + 1
print(" " * space, end="")
print("^" * row)
index += 1
# Print the trunk
index = 0
while index < th:
space = bh - 1
print(" " * space + "X")
index += 1
Read in the control inputs
Print out the tree body
Print the trunk
Draw the CFG
bh = int(input("tree body height: "))
th = int(input("tree trunk height:" ))
# Print the tree
index = 0
while (index < bh):
space = bh - index - 1
row = index * 2 + 1
print(" " * space, end="")
print("^" * row)
index += 1
# Print the trunk
index = 0
while index < th:
space = bh - 1
print(" " * space + "X")
index += 1
ICA
Draw the CFG
bh = int(input("tree body height: "))
th = int(input("tree trunk height:" ))
# Print the tree
index = 0
while (index < bh):
space = bh - index - 1
row = index * 2 + 1
print(" " * space, end="")
print("^" * row)
index += 1
# Print the trunk
index = 0
while index < th:
space = bh - 1
print(" " * space + "X")
index += 1
How many nodes are there?
ICA
Draw the CFG
bh = int(input("tree body height: "))
th = int(input("tree trunk height:" ))
# Print the tree
index = 0
while (index < bh):
space = bh - index - 1
row = index * 2 + 1
print(" " * space, end="")
print("^" * row)
index += 1
# Print the trunk
index = 0
while index < th:
space = bh - 1
print(" " * space + "X")
index += 1
How many nodes are there?
How many edges (arrows) are there?
ICA
Draw the CFG
bh = int(input("tree body height: "))
th = int(input("tree trunk height:" ))
# Print the tree
index = 0
while (index < bh):
space = bh - index - 1
row = index * 2 + 1
print(" " * space, end="")
print("^" * row)
index += 1
# Print the trunk
index = 0
while index < th:
space = bh - 1
print(" " * space + "X")
index += 1
How many nodes are there?
How many edges (arrows) are there?
How many possible “paths” are there from start to finish?
ICA
Combine loops, multiplication
*
***
*****
*******
X
X
*
***
*****
*******
*********
X
*
***
*****
*******
*********
***********
*************
X
Solution
bh = int(input("tree body height: "))
th = int(input("tree trunk height:" ))
tree_width = bh * 2 + 1
# Print the tree
index = 0
while (index < bh):
space = bh - index - 1
row = index * 2 + 1
index2 = 0
while (index2 < space):
print(" ", end="")
index2+=1
index2 = 0
while (index2 < row):
print("^", end="")
index2+=1
print()
index += 1
# Print the trunk
index = 0
space = bh - 1
while index < th:
index2 = 0
while (index2 < space):
print(" ", end="")
index2+=1
print("X")
index += 1
Draw the CFG
# Print the trunk
index = 0
space = bh - 1
while index < th:
index2 = 0
while (index2 < space):
print(" ", end="")
index2+=1
print("X")
index += 1
bh = int(input("tree body height: "))
th = int(input("tree trunk height:" ))
tree_width = bh * 2 + 1
# Print the tree
index = 0
while (index < bh):
space = bh - index - 1
row = index * 2 + 1
index2 = 0
while (index2 < space):
print(" ", end="")
index2+=1
index2 = 0
while (index2 < row):
print("^", end="")
index2+=1
print()
index += 1
ICA
Diamonds
/\
/ \
\ /
\/
/\
/ \
/ \
\ /
\ /
\/
/\
/ \
/ \
/ \
\ /
\ /
\ /
\/
Solution
Solution
Read the input
Solution
diamond_height = int(input("Size of diamond point: "))
Read the input
Solution
diamond_height = int(input("Size of diamond point: "))
Read the input
Compute values
Solution
diamond_height = int(input("Size of diamond point: "))
dw = diamond_height
dhh = int(diamond_height / 2)
Read the input
Compute values
Solution
diamond_height = int(input("Size of diamond point: "))
dw = diamond_height
dhh = int(diamond_height / 2)
Read the input
Compute values
Print the “up” point
Solution
diamond_height = int(input("Size of diamond point: "))
dw = diamond_height
dhh = int(diamond_height / 2)
index = 0
while (index < dhh):
space_a = int(dw/2) - index - 1
space_b = index * 2
print( (" " * space_a) +"/" + (" " * space_b) + "\\")
index += 1
Read the input
Compute values
Print the “up” point
Solution
diamond_height = int(input("Size of diamond point: "))
dw = diamond_height
dhh = int(diamond_height / 2)
index = 0
while (index < dhh):
space_a = int(dw/2) - index - 1
space_b = index * 2
print( (" " * space_a) +"/" + (" " * space_b) + "\\")
index += 1
Read the input
Compute values
Print the “up” point
Print the “down” point
Solution
diamond_height = int(input("Size of diamond point: "))
dw = diamond_height
dhh = int(diamond_height / 2)
index = 0
while (index < dhh):
space_a = int(dw/2) - index - 1
space_b = index * 2
print( (" " * space_a) +"/" + (" " * space_b) + "\\")
index += 1
index = dhh-1
while (index >= 0):
space_a = int(dw/2) - index - 1
space_b = index * 2
print( (" " * space_a) +"\\" + (" " * space_b) + "/")
index -= 1
Read the input
Compute values
Print the “up” point
Print the “down” point
Solution
diamond_height = int(input("Size of diamond point: "))
dw = diamond_height
dhh = int(diamond_height / 2)
index = 0
while (index < dhh):
space_a = int(dw/2) - index - 1
space_b = index * 2
print( (" " * space_a) +"/" + (" " * space_b) + "\\")
index += 1
index = dhh-1
while (index >= 0):
space_a = int(dw/2) - index - 1
space_b = index * 2
print( (" " * space_a) +"\\" + (" " * space_b) + "/")
index -= 1
Read the input
Compute values
Print the “up” point
Print the “down” point
Similarities?
Solution
diamond_height = int(input("Size of diamond point: "))
dw = diamond_height
dhh = int(diamond_height / 2)
index = 0
while (index < dhh):
space_a = int(dw/2) - index - 1
space_b = index * 2
print( (" " * space_a) +"/" + (" " * space_b) + "\\")
index += 1
index = dhh-1
while (index >= 0):
space_a = int(dw/2) - index - 1
space_b = index * 2
print( (" " * space_a) +"\\" + (" " * space_b) + "/")
index -= 1
Read the input
Compute values
Print the “up” point
Print the “down” point
Similarities?
Draw the CFG
diamond_height = int(input("Size of diamond point: "))
dw = diamond_height
dhh = int(diamond_height / 2)
index = 0
while (index < dhh):
space_a = int(dw/2) - index - 1
space_b = index * 2
print( (" " * space_a) +"/" + (" " * space_b) + "\\")
index += 1
index = dhh-1
while (index >= 0):
space_a = int(dw/2) - index - 1
space_b = index * 2
print( (" " * space_a) +"\\" + (" " * space_b) + "/")
index -= 1
ICA
Draw the CFG
diamond_height = int(input("Size of diamond point: "))
dw = diamond_height
dhh = int(diamond_height / 2)
index = 0
while (index < dhh):
space_a = int(dw/2) - index - 1
space_b = index * 2
print( (" " * space_a) +"/" + (" " * space_b) + "\\")
index += 1
index = dhh-1
while (index >= 0):
space_a = int(dw/2) - index - 1
space_b = index * 2
print( (" " * space_a) +"\\" + (" " * space_b) + "/")
index -= 1
How many nodes are there?
How many edges (arrows) are there?
How many possible “paths” are there from start to finish?
ICA