From f6f18eab5d424729c7e10481deb48f09d6434ccc Mon Sep 17 00:00:00 2001 From: Stephen Rosen Date: Mon, 30 Nov 2026 17:03:27 -0600 Subject: [PATCH] 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. --- Doc/tutorial/controlflow.rst | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/Doc/tutorial/controlflow.rst b/Doc/tutorial/controlflow.rst index c397ed4f218b6a3..74323a2ba245f4d 100644 --- a/Doc/tutorial/controlflow.rst +++ b/Doc/tutorial/controlflow.rst @@ -201,9 +201,10 @@ iteration of the loop:: :keyword:`!else` Clauses on Loops ================================= -In a :keyword:`!for` or :keyword:`!while` loop the :keyword:`!break` statement -may be paired with an :keyword:`!else` clause. If the loop finishes without -executing the :keyword:`!break`, the :keyword:`!else` clause executes. +A :keyword:`!for` or :keyword:`!while` loop may be paired with an +:keyword:`!else` clause, often with a :keyword:`break` inside the loop. +If the loop finishes without executing the :keyword:`!break`, the +:keyword:`!else` clause executes. In a :keyword:`for` loop, the :keyword:`!else` clause is executed after the loop finishes its final iteration, that is, if no break occurred. @@ -240,16 +241,16 @@ which searches for prime numbers:: the ``for`` loop, **not** the ``if`` statement.) One way to think of the else clause is to imagine it paired with the ``if`` -inside the loop. As the loop executes, it will run a sequence like -if/if/if/else. The ``if`` is inside the loop, encountered a number of times. If +inside the loop. The ``if`` is inside the loop, encountered a number of times. If the condition is ever true, a ``break`` will happen. If the condition is never true, the ``else`` clause outside the loop will execute. -When used with a loop, the ``else`` clause has more in common with the ``else`` -clause of a :keyword:`try` statement than it does with that of ``if`` -statements: a ``try`` statement's ``else`` clause runs when no exception -occurs, and a loop's ``else`` clause runs when no ``break`` occurs. For more on -the ``try`` statement and exceptions, see :ref:`tut-handling`. +The :keyword:`!else` clause of a :keyword:`!for` loop can also be understood +similarly to the :keyword:`!else` clause of a :keyword:`try` statement: a +:keyword:`!try` statement's :keyword:`!else` clause runs when no exception +occurs, and a :keyword:`!for` loop's :keyword:`!else` clause runs when no +:keyword:`!break` occurs. For more on the :keyword:`!try` statement and +exceptions, see :ref:`tut-handling`. .. index:: single: ...; ellipsis literal .. _tut-pass: