Note 6.2.1.
The names of the variables have been chosen to help readability.
while
Statement
while
statement. The while
statement provides a much more general mechanism for iterating. Similar to the if
statement, it uses a boolean expression to control the flow of execution. The body of while will be repeated as long as the controlling boolean expression evaluates to True
.while
loop to create any type of iteration we wish, including anything that we have previously done with a for
loop. For example, the program in the previous section could be rewritten using while
. Instead of relying on the range
function to produce the numbers for our summation, we will need to produce them ourselves. To do this, we will create a variable called aNumber
and initialize it to 1, the first number in the summation. Every iteration will add aNumber
to the running total until all the values have been used. In order to control the iteration, we must create a boolean expression that evaluates to True
as long as we want to keep adding values to our running total. In this case, as long as aNumber
is less than or equal to the bound, we should keep going.while
statement as if it were in natural language. It means, while aNumber
is less than or equal to aBound
, continue executing the body of the loop. Within the body, each time, update theSum
using the accumulator pattern and increment aNumber
. After the body of the loop, we go back up to the condition of the while
and reevaluate it. When aNumber
becomes greater than aBound
, the condition fails and flow of control continues to the return
statement.while
statement:False
or True
.False
, exit the while
statement and continue execution at the next statement.True
, execute each of the statements in the body and then go back to step 1.False
the first time through the loop, the statements inside the loop are never executed.False
and the loop terminates. Otherwise the loop will repeat forever. This is called an infinite loop. An endless source of amusement for computer scientists is the observation that the directions written on the back of the shampoo bottle (lather, rinse, repeat) create an infinite loop.aBound
is finite, and we can see that the value of aNumber
increments each time through the loop, so eventually it will have to exceed aBound
. In other cases, it is not so easy to tell.for
statement will always iterate through a sequence of values like the list of names for the party or the list of numbers created by range
. Since we know that it will iterate once for each value in the collection, it is often said that a for
loop creates a definite iteration because we definitely know how many times we are going to iterate. On the other hand, the while
statement is dependent on a condition that needs to evaluate to False
in order for the loop to terminate. Since we do not necessarily know when this will happen, it creates what we call indefinite iteration. Indefinite iteration simply means that we don’t know how many times we will repeat but eventually the condition controlling the iteration will fail and the iteration will stop. (Unless we have an infinite loop which is of course a problem)while
loop is more work for you — the programmer — than the equivalent for
loop. When using a while
loop you have to control the loop variable yourself. You give it an initial value, test for completion, and then make sure you change something in the body so that the loop terminates. That also makes a while loop harder to read and understand than the equivalent for loop. So, while you can implement definite iteration with a while loop, it’s not a good idea to do that. Use a for loop whenever it will be known at the beginning of the iteration process how many times the block of code needs to be executed.n = 10
answer = 1
while ( n > 0 ):
answer = answer + n
n = n + 1
print(answer)
total_score
. If the counter is an odd number, subtract half its value from total_score
.stop_at_one
that takes an integer as a parameter, and performes the following operations on it until the integer equals 1: If the number is odd, multiply by 3 and add 1. If the number is even, divide by two. The function should then return how many iterations it took to reach 1.