Skip to content

Commit 56467a6

Browse files
miss-islingtonnedbatJelleZijlstra
authored
[3.13] For-else deserves its own section in the tutorial (GH-123946) (#124564)
For-else deserves its own section in the tutorial (GH-123946) * For-else deserves its own section in the tutorial * remove mention of unrolling the loop * Update Doc/tutorial/controlflow.rst --------- (cherry picked from commit ffdc80e) Co-authored-by: Ned Batchelder <[email protected]> Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 2f25d85 commit 56467a6

File tree

1 file changed

+54
-31
lines changed

1 file changed

+54
-31
lines changed

Doc/tutorial/controlflow.rst

+54-31
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,52 @@ arguments. In chapter :ref:`tut-structures`, we will discuss in more detail abo
160160

161161
.. _tut-break:
162162

163-
:keyword:`!break` and :keyword:`!continue` Statements, and :keyword:`!else` Clauses on Loops
164-
============================================================================================
163+
:keyword:`!break` and :keyword:`!continue` Statements
164+
=====================================================
165165

166166
The :keyword:`break` statement breaks out of the innermost enclosing
167-
:keyword:`for` or :keyword:`while` loop.
167+
:keyword:`for` or :keyword:`while` loop::
168168

169-
A :keyword:`!for` or :keyword:`!while` loop can include an :keyword:`!else` clause.
169+
>>> for n in range(2, 10):
170+
... for x in range(2, n):
171+
... if n % x == 0:
172+
... print(f"{n} equals {x} * {n//x}")
173+
... break
174+
...
175+
4 equals 2 * 2
176+
6 equals 2 * 3
177+
8 equals 2 * 4
178+
9 equals 3 * 3
179+
180+
The :keyword:`continue` statement continues with the next
181+
iteration of the loop::
182+
183+
>>> for num in range(2, 10):
184+
... if num % 2 == 0:
185+
... print(f"Found an even number {num}")
186+
... continue
187+
... print(f"Found an odd number {num}")
188+
...
189+
Found an even number 2
190+
Found an odd number 3
191+
Found an even number 4
192+
Found an odd number 5
193+
Found an even number 6
194+
Found an odd number 7
195+
Found an even number 8
196+
Found an odd number 9
197+
198+
.. _tut-for-else:
199+
200+
:keyword:`!else` Clauses on Loops
201+
=================================
202+
203+
In a :keyword:`!for` or :keyword:`!while` loop the :keyword:`!break` statement
204+
may be paired with an :keyword:`!else` clause. If the loop finishes without
205+
executing the :keyword:`!break`, the :keyword:`!else` clause executes.
170206

171207
In a :keyword:`for` loop, the :keyword:`!else` clause is executed
172-
after the loop reaches its final iteration.
208+
after the loop finishes its final iteration, that is, if no break occurred.
173209

174210
In a :keyword:`while` loop, it's executed after the loop's condition becomes false.
175211

@@ -198,32 +234,19 @@ which searches for prime numbers::
198234
9 equals 3 * 3
199235

200236
(Yes, this is the correct code. Look closely: the ``else`` clause belongs to
201-
the :keyword:`for` loop, **not** the :keyword:`if` statement.)
202-
203-
When used with a loop, the ``else`` clause has more in common with the
204-
``else`` clause of a :keyword:`try` statement than it does with that of
205-
:keyword:`if` statements: a :keyword:`try` statement's ``else`` clause runs
206-
when no exception occurs, and a loop's ``else`` clause runs when no ``break``
207-
occurs. For more on the :keyword:`!try` statement and exceptions, see
208-
:ref:`tut-handling`.
209-
210-
The :keyword:`continue` statement, also borrowed from C, continues with the next
211-
iteration of the loop::
212-
213-
>>> for num in range(2, 10):
214-
... if num % 2 == 0:
215-
... print("Found an even number", num)
216-
... continue
217-
... print("Found an odd number", num)
218-
...
219-
Found an even number 2
220-
Found an odd number 3
221-
Found an even number 4
222-
Found an odd number 5
223-
Found an even number 6
224-
Found an odd number 7
225-
Found an even number 8
226-
Found an odd number 9
237+
the ``for`` loop, **not** the ``if`` statement.)
238+
239+
One way to think of the else clause is to imagine it paired with the ``if``
240+
inside the loop. As the loop executes, it will run a sequence like
241+
if/if/if/else. The ``if`` is inside the loop, encountered a number of times. If
242+
the condition is ever true, a ``break`` will happen. If the condition is never
243+
true, the ``else`` clause outside the loop will execute.
244+
245+
When used with a loop, the ``else`` clause has more in common with the ``else``
246+
clause of a :keyword:`try` statement than it does with that of ``if``
247+
statements: a ``try`` statement's ``else`` clause runs when no exception
248+
occurs, and a loop's ``else`` clause runs when no ``break`` occurs. For more on
249+
the ``try`` statement and exceptions, see :ref:`tut-handling`.
227250

228251
.. _tut-pass:
229252

0 commit comments

Comments
 (0)