|
CS 140: Introduction
to Computer Programming
For Loops
Performing certain tasks over and over again are
another one of those things that computers do
well. For instance, we might want a
program to execute a block of code a fixed
number of times. So to print the lyrics to
a certain well-known song, we would type
for j in range(1,4):
print "We wish you a Merry
Christmas,"
But wait, you protest, this should print the
line four times, and we only want three of
them! In fact, due to a peculiarity of how
Python interprets this code (it counts all the
way up to 4, but does not include 4), the print
statement will execute only three times.
Notice that we use a colon, just as with if
and else
statements, because the results of a for
command depend on the lines following it.
And just as before, all the indented lines
immediately following a for
command are the ones that get repeated.
This has important consequences, as the
following two examples illustrate.
BAD
for j in
range(1,4):
print "We wish you a Merry
Christmas,"
print "and a Happy New Year!"
GOOD
for j in
range(1,4):
print "We wish you a Merry
Christmas,"
print "and a Happy New Year!"
BEST
for j in
range(1,4):
print "We wish you a Merry
Christmas,"
print "and a Happy New Year!"
Because both print statements are indented, the
first version will print both lines three times,
which is not what we want. The second
version gets the job done, but the third version
is even better since it separates the last line
visually, making it abundantly clear that it is
not part of the for loop.
The variable j
that is keeping track of which time we are going
through the for loop can be used in the body of
the loop. This is rather convenient, say,
for counting from 1 to 100.
for j in range(1,101):
print j,
The comma after the j
will cause all the numbers to be printed one
after the other on the same line, which makes it
easier to see them all. In order to skip
all the multiples of 3, just check before
printing to make sure that the remainder when j
is divided by 3 isn't 0, like this.
for j in range(1,101):
if
j%3 != 0:
print
j,
If you would rather think about this in terms of
when j
is a multiple of 3, you could type
for j in range(1,101):
if
j%3 == 0:
pass
else:
print
j,
Here we have introduced a new command called pass,
which literally means "do nothing."
Finally, it's worth mentioning that Python can
count in more sophisticated ways than simply
increasing by 1 each time. This is done by
indicating a step size in the
range. So the code
for j in range(1,101,2):
print j,
will print all the numbers from 1 to 100
starting at 1 and increasing by 2 each
time. In other words, it will print all
the odd numbers from 1 to 99. To get all
the even numbers instead, you would need to
start counting at 2 instead, like this.
for j in range(2,101,2):
print j,
Python can even count backwards. To
perform an exciting countdown, type
for j in range(10,0,-1):
print j,
print "Blast off!"
Observe that we did not type range(11,1,-1)
as this would have started counting at 11 and
stopped before it reached 1.
|
|
|