Comparing For-Loops and While-Loops
Let’s take a look at a few examples of how for-loops and while-loops can be used to solve the same problems. Plus we’ll see a simple program that can’t be written using a for-loop.
Factorials are numbers of the form 1 × 2 × 3 × ... × n, and they tell you how many ways n objects can be arranged in a line. For example, the letters ABCD can be arranged in 1 × 2 × 3 × 4 = 24 different ways. Here’s one way to calculate factorials using a for-loop:
# forfact.py n = int(input('Enter an integer >= 0: ')) fact = 1 for i in range(2, n + 1): fact = fact * i print(str(n) + ' factorial is ' + str(fact))
Here’s another way to do it using a while-loop:
# whilefact.py n = int(input('Enter an integer >= 0: ')) fact = 1 i = 2 while i <= n: fact = fact * i i = i + 1 print(str(n) + ' factorial is ' + str(fact))
Both of these programs behave the same from the user’s perspective, but the internals are quite different. As is usually the case, the while-loop version is a little more complicated than the for-loop version.
Summing numbers from the user
The following programs ask the user to enter some numbers, and then return their sum. Here is a version using a for-loop:
# forsum.py n = int(input('How many numbers to sum? ')) total = 0 for i in range(n): s = input('Enter number ' + str(i + 1) + ': ') total = total + int(s) print('The sum is ' + str(total))
Here’s a program that does that same thing using a while-loop:
# whilesum.py n = int(input('How many numbers to sum? ')) total = 0 i = 1 while i <= n: s = input('Enter number ' + str(i) + ': ') total = total + int(s) i = i + 1 print('The sum is ' + str(total))
Again, the while-loop version is a little more complex than the for-loop version.
Summing an unknown number of numbers
Now here’s something that can’t be done with the for-loops we’ve introduced so far. Suppose we want to let users enter a list of numbers to be summed without asking them ahead of time how many numbers they have. Instead, they just type the string 'done' when they have no more numbers to add. Here’s how to do it using a while-loop:
# donesum.py total = 0 s = input('Enter a number (or "done"): ') while s != 'done': num = int(s) total = total + num s = input('Enter a number (or "done"): ') print('The sum is ' + str(total))
The idea here is to keep asking users to enter a number, quitting only when they enter 'done'. The program doesn’t know ahead of time how many times the loop body will be executed.
Notice a few more details:
- We must call input in two different places: before the loop and inside the loop body. This is necessary because the loop condition decides whether or not the input is a number or 'done'.
- The ordering of the statements in the loop body is very important. If the loop condition is True, then we know s is not 'done', and so we assume it is an integer. Thus we can convert it to an integer, add it to the running total, and then ask the user for more input.
We convert the input string s to an integer only after we know s is not the string 'done'. If we had written
s = int(input('Enter a number (or "done"): '))
as we had previously, the program would crash when the user typed 'done'.
- There is no need for the i counter variable anymore. In the previous summing programs, i was needed to track how many numbers had been entered so far. As a general rule of thumb, a program with fewer variables is easier to read, debug, and extend.