Computer Science 15-110, Spring 2011
Class Notes: Loops
- "for" loops
- "for" loop over a range
- Range from 0 to y: use range(y+1)
- Range from x to y: use range(x, y+1)
- Range from x to y with step z: use range(x, y+1, z)
- Range in reverse (use a negative step)
- "for" loop over a String
- With an index
- Without an index
- "while" loops
- Basic Syntax and Use
- Read Until a Sentinel
- Read Until a Condition
- Incorrect Usage: Iterate Over a Range of Integers
- Nested loops
Loops
"for" loops
- "for" loop over a range
- Range from 0 to y: use range(y+1)
# print the numbers from 0 to 4 (not 5)
for x in range(5):
print x
- Range from x to y: use range(x, y+1)
# print the numbers from 10 to 14 (not 15)
for x in range(10, 15):
print x
- Range from x to y with step z: use range(x, y+1, z)
# print the even numbers from 10 to 14 (not 15)
for x in range(10, 15, 2):
print x
- Range in reverse (use a negative step):
# print the even numbers from 14 to 10 (not 9)
for x in range(14, 9, -2):
print x
- "for" loop over a String
- With an index
# print each character in a String
s = "Amazing!"
for i in range(len(s)):
print s[i]
- Without an index
# print each character in a String
for ch in "Wow!":
print ch
- "while" loops
- Basic Syntax and Use
while (test):
body
For example:
def beHappy():
happy = False
while (not happy):
response = raw_input("Are you happy yet? [yes or no] ")
happy = (response == "yes")
print "Well, now I'm happy, too!"
Another example:
def sumOfDigits(n):
sum = 0
n = abs(n)
while (n != 0):
sum += (n % 10)
n /= 10
return sum
- Read Until a Sentinel
# Keep reading names until the user enters "done"
count = 0
sentinel = "done"
input = None
print "Keep entering names. To quit, enter", sentinel
while (input != sentinel):
input = raw_input("Enter name #" + str(count+1) + ": ")
if (input != sentinel):
count += 1
print "You entered", count, "names!"
- Read Until a Condition
# keep reading numbers until their sum exceeds 10
sum = 0.0
while (sum < 10):
print "Sum = " + str(sum) + ". ",
input = float(raw_input("Next number --> "))
sum += input
print "Final sum = ", sum
- Incorrect Usage: Iterate Over a Range of Integers
# sum numbers from 1 to 10
# note: you should use a "for" loop here, not a "while" loop
sum = 0
counter = 1
while (counter <= 10):
sum += counter
counter += 1
print "The sum from 1 to 10 is", sum
Once again, but with a bug!:
# sum numbers from 1 to 10
# warning: buggy version!
sum = 0
counter = 0
while (counter <= 10):
counter += 1
sum += counter
print "The sum from 1 to 10 is", sum
And once more, using a "for" loop:
# sum numbers from 1 to 10
sum = 0
for counter in range(1,11):
sum += counter
print "The sum from 1 to 10 is", sum
- Nested loops
n = int(raw_input("How many rows? "))
for row in range(n):
for col in range(n):
print "*",
print
Another example:
n = int(raw_input("How many rows? "))
for row in range(n):
for col in range(row+1):
print "*",
print
Again, with a useful helper function:
def printStars(starCount):
for col in range(starCount):
print "*",
print
n = int(raw_input("How many rows? "))
for row in range(n):
printStars(row+1)
carpe diem -
carpe diem - carpe diem - carpe diem
- carpe diem - carpe diem -
carpe diem - carpe diem - carpe
diem