Lecture 06 Loops

Joseph Haugh

University of New Mexico

Free Recall

  • Get out a sheet of paper or open a text editor
  • For 2 minutes write down whatever comes to mind about the last class
    • This could be topics you learned
    • Questions you had
    • Connections you made

Start Reading

  • Let’s start by reading chapter 7C
  • Take about 10-15 minutes to do this

Repeating Tasks

  • So far you have only written programs which perform a single set of operations
  • This is is only useful up to a point
  • Imagine if every time you opened spotify it asked which song you wanted to play, played it then exited
  • That would be very annoying!
  • Instead you want it stay open and keep responding to your requests
  • This is where looping come into play

While Loops

  • There are a few different kinds of loops
  • The simplest is the while loop
  • It allows us to repeat a set of statements until the condition is False
  • while loops have the following syntax:
while <<condition>>:
    <<statements>>

While loops

while <<condition>>:
    <<statements>>
  • Let’s try a simple example where we print out Hello! a certain number of times:
n = 0
while n <= 3:
    print("Hello!")
    n += 1

Visualize

How many times will Hello! be printed?

Another Example

while <<condition>>:
    <<statements>>

Let’s try another example:

sum_x = 0
n = 3
while n > 0:
    x = int(input())
    sum_x += x
    n -= 1
print(sum_x)

Visualize

What is this code doing?

When To Stop

while <<condition>>:
    <<statements>>

The stop condition can also depend on input to the program:

x = int(input())
while x != 7:
    x = int(input())
print(x)

Visualize

For Loops

  • In addition to while loops Python also gives us for loops
  • for loops perform the same task as while loops, repeating statements
  • However, they make some kinds of loops easier
  • for loops have the following syntax:
for <<variableName>> in range(<<startValue>>, <<endValue>>):
    <<statements>>

For Loops

for <<variableName>> in range(<<startValue>>, <<endValue>>):
    <<statements>>
  • for loops allow us to easily express looping over a range of numbers
  • The first iteration of the loop sets variableName equal to startValue
  • On the next iteration variableName is set to startValue+1
  • The next iteration variableName is set to startValue+2 etc.
  • This goes on until variableName has the endValue-1, after which it stops
  • Note this means variableName will never actually be set to endValue

Rewriting First Example

for <<variableName>> in range(<<startValue>>, <<endValue>>):
    <<statements>>

Remember our print Hello! example? Lets write it with while and for:

n = 0
while n <= 3:
    print("Hello!")
    n += 1

Visualize

for n in range(0, 4):
    print("Hello!")

Visualize

Another Example

for <<variableName>> in range(<<startValue>>, <<endValue>>):
    <<statements>>

What does the following code do?

sum_x = 0
for x in range(7, 11):
    print(x)
    sum_x += x
print(sum_x)

Visualize

Changing Range Step

for <<variableName>> in range(<<startValue>>, <<endValue>>):
    <<statements>>

You can also make the range function change the variable by a different amount each iteration:

for x in range(2, 10, 2):
    print(x)

Visualize

Will 10 be printed?

Counting Down

for <<variableName>> in range(<<startValue>>, <<endValue>>):
    <<statements>>

You can also count down:

for x in range(10, 2, -2):
    print(x)

Visualize

Will 2 be printed?