Skip to content

Commit f6f18ea

Browse files
committed
Improve explanation of (for|while)-else in tutorial
- Remove the sentence that sparked some confusion, which starts "As the loop executes ..." - The opening of "else Clauses on Loops" now states that the `else` is paired with the `for` or `while` loop keyword, not with an internal `break` -- this small change better prepares readers for the possibility of `while-else` and `for-else` without a `break` (which is rare but valid). - The final paragraph of this doc section has been reworded, so that it is clear that it, and the section above it, are both analogies which can help readers understand `for-else` and `while-else`. The precise rules for what the `else` clause does are properly stated above.
1 parent 846acf1 commit f6f18ea

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

‎Doc/tutorial/controlflow.rst‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,10 @@ iteration of the loop::
201201
:keyword:`!else` Clauses on Loops
202202
=================================
203203

204-
In a :keyword:`!for` or :keyword:`!while` loop the :keyword:`!break` statement
205-
may be paired with an :keyword:`!else` clause. If the loop finishes without
206-
executing the :keyword:`!break`, the :keyword:`!else` clause executes.
204+
A :keyword:`!for` or :keyword:`!while` loop may be paired with an
205+
:keyword:`!else` clause, often with a :keyword:`break` inside the loop.
206+
If the loop finishes without executing the :keyword:`!break`, the
207+
:keyword:`!else` clause executes.
207208

208209
In a :keyword:`for` loop, the :keyword:`!else` clause is executed
209210
after the loop finishes its final iteration, that is, if no break occurred.
@@ -240,16 +241,16 @@ which searches for prime numbers::
240241
the ``for`` loop, **not** the ``if`` statement.)
241242

242243
One way to think of the else clause is to imagine it paired with the ``if``
243-
inside the loop. As the loop executes, it will run a sequence like
244-
if/if/if/else. The ``if`` is inside the loop, encountered a number of times. If
244+
inside the loop. The ``if`` is inside the loop, encountered a number of times. If
245245
the condition is ever true, a ``break`` will happen. If the condition is never
246246
true, the ``else`` clause outside the loop will execute.
247247

248-
When used with a loop, the ``else`` clause has more in common with the ``else``
249-
clause of a :keyword:`try` statement than it does with that of ``if``
250-
statements: a ``try`` statement's ``else`` clause runs when no exception
251-
occurs, and a loop's ``else`` clause runs when no ``break`` occurs. For more on
252-
the ``try`` statement and exceptions, see :ref:`tut-handling`.
248+
The :keyword:`!else` clause of a :keyword:`!for` loop can also be understood
249+
similarly to the :keyword:`!else` clause of a :keyword:`try` statement: a
250+
:keyword:`!try` statement's :keyword:`!else` clause runs when no exception
251+
occurs, and a :keyword:`!for` loop's :keyword:`!else` clause runs when no
252+
:keyword:`!break` occurs. For more on the :keyword:`!try` statement and
253+
exceptions, see :ref:`tut-handling`.
253254

254255
.. index:: single: ...; ellipsis literal
255256
.. _tut-pass:

0 commit comments

Comments
 (0)