Python Concepts/While Statement

Objective

 * Learn about Python's loop statement,.
 * Learn about three dependent statements,,  , and.

The While Statement
Although the  statement from the previous lesson can serve many purposes, it isn't good at being recursive. This means that the said statement cannot loop over and over again. This is where the  statement comes into play. This statement will execute its code block over and over again until its conditions are false. This can be useful if something needs to be repeated over and over for a said amount of time. Some examples are given below.

A  statement's condition is always true or false, like the   statement. This means Boolean math can be used to control the looping. Since  will never execute, what will   do? It will infinitely loop. This will be important later; we'll spend more time on it later in the lesson.

The Else Statement
Unlike a lot of other computer languages, Python allows you to use an  statement in conjunction with a   statement. When a while statement finishes its loop naturally, it will then execute the  statement. If the  statement stops prematurely for any reason, it will not execute the   statement. An example is given below.

In the second example,  is used to generate a. Because the  statement ended prematurely, the   statement was never executed.

The Break Statement
There can be times when you want to prematurely end a  statement. The  statement can be used to accomplish this. The  statement can be used to prematurely end the   statement that it's nested in. This means it will end only one  statement, not all of them. An example is given below.

The above example demonstrates that a  loop can be ended, even if it is. You should also note from the example that the  statement doesn't require a colon, which means new indentation isn't needed. Putting a colon at the end of  will cause an error; the omission of the colon isn't optional, it's mandatory.

Continue Statement
Although the  statement comes in handy, the   statement is just as useful. Unlike the  statement, the   statement will stop the current execution of code and go back to the beginning of the   statement. This can be used to skip part of the code and not end the loop at the same time. The  statement is used just like the   statement. An example is given below.

Although the example could have easily omitted the, it demonstrates how it works. The  statement is used to reduce the amount of indenting and nesting used; it would be easier to check five conditions and use a   if they fail, than to nest five   statements. This can reduce the amount of typing needed and, as a result, will increase readability and code clarity.

Detecting Abnormal Termination
If the  statement terminates normally, the   statement is executed:

10

9

8

7

6

5

normal termination

If the  statement terminates abnormally, the   statement is not executed. The following code detects abnormal termination:

10

9

8

7

6

5

abnormal termination

"Normal" termination
The following code produces four examples of "Normal" termination:

initial c = -4

normal termination. c = -4

initial c = 0

c = 0

c = 1

c = 2

c = 3

c = 4

normal termination. c = 5 # This is probably what you wanted.

initial c = 3

c = 3

c = 4

normal termination. c = 5

initial c = 19

normal termination. c = 19

The following code also produces "Normal" termination: initial c = 0

c = 0

c = 1

c = 2

c = 3

normal termination. c = 238

For normal termination we expect  to have the value 5. Try again:

initial c = -4

initial c = 0

c = 0

c = 1

c = 2

c = 3

c = 4

normal termination. c = 5

initial c = 3

c = 3

c = 4

normal termination. c = 5

initial c = 19

Even this code is not fool-proof. Try again:

initial c = -4

initial c = 0

c = 0

c = 1

c = 2

c = 3

c = 4

normal termination. c = 5

initial c = 3

c = 3

c = 4

initial c = 19

You can see that controlling the performance and behavior of loops is not a trivial matter.

The statement
It seems that the  statement in the next block:

performs the same function as:

For example:

and have the same functionality.

Nested statements
statements are nested when the body of one  statement contains another   statement: 000 #

001 #

... # 1000 numbers from 000 through 999

998 #

999 #

normal termination

In the body of the innermost  statement above there is a   statement. What if there were at this point a condition that required the immediate termination of all  statements? Here is one way to do it: 000 #

001 #

... # 244 numbers from 000 through 243

242 #

243 #

normal termination

flag = False # This indicates abnormal termination.

Python's ability to handle errors adds some elegance to the solution:

000

001

002

....

241

242

243

ZeroDivisionError

The  statement will be presented in a later lesson.

Programming style
Avoid hidden endless loops such as:

or

or

Avoid loops that will never iterate:

In some of the examples above the incrementer was at the bottom of the loop:

As the body of the loop gets bigger, the importance of the incrementer remains but becomes less obvious. The following code with the incrementer at the top does the same job:

With the incrementer at the top you can put more code into the body of the loop without having to consider the incrementer.

For rigorous control of the loop you might consider: c = 0

c = 1

c = 2

c = 3

c = 4

Normal termination

Examples
Given  remove all negative values from list

Take care when code in the body of the loop changes a condition of the loop. Reverse the order of test and delete, and you can ignore whatever may have happened behind you:

The simple equivalent of a  statement:

Assignments

 * Work with the  statement and get a general feeling of how it works.
 * PEP 315 proposed that an optional  clause should be added to the   statement, although it was ultimately rejected. Read PEP 315. Why do you think it was rejected? How would this affect readability?

Further Reading or Review

 * Previous Lesson: Match-case Statement
 * This Lesson: While Statement
 * Next Lesson: For Statement
 * Course Home Page