|
4 | 4 | More Control Flow Tools
|
5 | 5 | ***********************
|
6 | 6 |
|
7 |
| -Besides the :keyword:`while` statement just introduced, Python uses the usual |
8 |
| -flow control statements known from other languages, with some twists. |
| 7 | +As well as the :keyword:`while` statement just introduced, Python uses a few more |
| 8 | +that we will encounter in this chapter. |
9 | 9 |
|
10 | 10 |
|
11 | 11 | .. _tut-if:
|
@@ -163,14 +163,21 @@ arguments. In chapter :ref:`tut-structures`, we will discuss in more detail abo
|
163 | 163 | :keyword:`!break` and :keyword:`!continue` Statements, and :keyword:`!else` Clauses on Loops
|
164 | 164 | ============================================================================================
|
165 | 165 |
|
166 |
| -The :keyword:`break` statement, like in C, breaks out of the innermost enclosing |
| 166 | +The :keyword:`break` statement breaks out of the innermost enclosing |
167 | 167 | :keyword:`for` or :keyword:`while` loop.
|
168 | 168 |
|
169 |
| -Loop statements may have an :keyword:`!else` clause; it is executed when the loop |
170 |
| -terminates through exhaustion of the iterable (with :keyword:`for`) or when the |
171 |
| -condition becomes false (with :keyword:`while`), but not when the loop is |
172 |
| -terminated by a :keyword:`break` statement. This is exemplified by the |
173 |
| -following loop, which searches for prime numbers:: |
| 169 | +A :keyword:`!for` or :keyword:`!while` loop can include an :keyword:`!else` clause. |
| 170 | + |
| 171 | +In a :keyword:`for` loop, the :keyword:`!else` clause is executed |
| 172 | +after the loop reaches its final iteration. |
| 173 | + |
| 174 | +In a :keyword:`while` loop, it's executed after the loop's condition becomes false. |
| 175 | + |
| 176 | +In either kind of loop, the :keyword:`!else` clause is **not** executed |
| 177 | +if the loop was terminated by a :keyword:`break`. |
| 178 | + |
| 179 | +This is exemplified in the following :keyword:`!for` loop, |
| 180 | +which searches for prime numbers:: |
174 | 181 |
|
175 | 182 | >>> for n in range(2, 10):
|
176 | 183 | ... for x in range(2, n):
|
|
0 commit comments