1 of 47

Lecture 8

While Loops

CSc 250 - Spring 2018

Benjamin Dicken

2 of 47

Repetition

  • It is often useful to have a way to quickly repeat lines of code in a program
  • In many cases, this can save us (a lot) of typing!

3 of 47

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.

4 of 47

While-loop simulation

  • How many times will the loop repeat (iterate) ?
  • What will be printed out?

counter = 0

while (counter < 5):

print("Hello World!")

counter = counter + 1

ICA

5 of 47

While-loop simulation

  • How many times will the loop repeat (iterate) ?
  • What will be printed out?

while (True):

print("Hello World!")

ICA

6 of 47

While-loop simulation

  • How many times will the loop repeat (iterate) ?
  • What will be printed out?

counter = 0

while (counter < 5 and False):

print("Hello World!")

counter = counter + 1

ICA

7 of 47

While-loop simulation

  • How many times will the loop repeat (iterate) ?
  • What will be printed out?

tracker = 0

increment = 3

while (tracker < 15):

print("Hello World!")

tracker = tracker + increment

ICA

8 of 47

While-loop simulation

  • How many times will the loop repeat (iterate) ?
  • What will be printed out?

a = 10

b = 0

while (a != b):

print("Hello World!")

a = a - 1

b = b + 1

ICA

9 of 47

Common While-loop usage

  • Though while-loops can be used in many ways, this is a common pattern

index = 0

while (index < 10):

print("hello there")

index = index + 1

10 of 47

Common While-loop usage

  • Though while-loops can be used in many ways, this is a common pattern

index = 0

while (index < 10):

print("hello there")

index = index + 1

Loop control variable

Loop limit

Loop control variable increment

11 of 47

Common While-loop usage

  • Though while-loops can be used in many ways, this is a common pattern
  • Alternate . . .

index = 0

while (index < 10):

print("hello there")

index += 1

12 of 47

Nesting an if-statement

  • If-statements can be placed within the body of a while-loop
  • (and vice-versa)

index = 0

while (index < 10):

if (index < 5):

print("less than 5")

else:

print("less than 10")

index += 1

13 of 47

Nesting an if-statement

  • If-statements can be placed within the body of a while-loop
  • (and vice-versa)
  • This is referred to as “nesting an if/else-statement inside of a while-loops”

index = 0

while (index < 10):

if (index < 5):

print("less than 5")

else:

print("less than 10")

index += 1

14 of 47

While-loop simulation

  • How many times will the loop repeat (iterate) ?
  • What will be printed out?

index = 0

while (index < 18):

if (index < 12)

index += 5

else:

index += 1

print("Hello World!")

ICA

15 of 47

While-loop simulation

  • How many times will the loop repeat (iterate) ?
  • What will be printed out?

index = 0

while (index < 20):

if (index < 14)

index += 4

print("Hello World!")

index += 1

ICA

16 of 47

Guess the name

  • Write a program that lets the user attempt to guess your name
  • Unlimited attempts
  • The output will be something like this:
  • Work in groups - help eachother out!

What is my name? Joe

-- Wrong, guess again --

What is my name? Eric

-- Wrong, guess again --

What is my name? Ben

-- Correct! Bye --

ICA

17 of 47

Guess the name

  • Now, change it so that there is a 4-guess limit
  • Work in groups - help eachother out!

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

18 of 47

Guess the name

  • Add a report
  • Work in groups - help eachother out!

...

----

You FAILED!

Attempts: 4

...

----

You PASSED!

Attempts: 2

ICA

19 of 47

Combine loops, multiplication

  • Let’s write a program that:
    • Allows the user to print out a tree of ANY size (not just SMALL, MEDIUM, or LARGE)
    • Can specify tree height and trunk height
    • Do this with Multiplication and while-loop(s)

*

***

*****

*******

X

X

*

***

*****

*******

*********

X

*

***

*****

*******

*********

***********

*************

X

20 of 47

The Solution

21 of 47

The Solution

Read in the control inputs

22 of 47

The Solution

bh = int(input("tree body height: "))

th = int(input("tree trunk height:" ))

Read in the control inputs

23 of 47

The Solution

bh = int(input("tree body height: "))

th = int(input("tree trunk height:" ))

Read in the control inputs

Print out the tree body

24 of 47

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

25 of 47

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

26 of 47

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

27 of 47

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

28 of 47

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

29 of 47

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

30 of 47

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

31 of 47

Combine loops, multiplication

  • Let’s write a program that:
    • Allows the user to print out a tree of ANY size (not just SMALL, MEDIUM, or LARGE)
    • Can specify tree height and trunk height
    • Now do it with NO multiplication - just while-loops

*

***

*****

*******

X

X

*

***

*****

*******

*********

X

*

***

*****

*******

*********

***********

*************

X

32 of 47

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

33 of 47

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

34 of 47

Diamonds

  • Let’s write a program that:
    • Allows the user to print out a diamond of any size
    • Can specify the side height
    • Do this with Multiplication and while-loop(s)

/\

/ \

\ /

\/

/\

/ \

/ \

\ /

\ /

\/

/\

/ \

/ \

/ \

\ /

\ /

\ /

\/

35 of 47

Solution

36 of 47

Solution

Read the input

37 of 47

Solution

diamond_height = int(input("Size of diamond point: "))

Read the input

38 of 47

Solution

diamond_height = int(input("Size of diamond point: "))

Read the input

Compute values

39 of 47

Solution

diamond_height = int(input("Size of diamond point: "))

dw = diamond_height

dhh = int(diamond_height / 2)

Read the input

Compute values

40 of 47

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

41 of 47

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

42 of 47

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

43 of 47

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

44 of 47

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?

45 of 47

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?

46 of 47

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

47 of 47

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