From 8bd79173905e1dffecab02890f608d730816355d Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Thu, 26 Sep 2024 07:47:10 -0400 Subject: [PATCH 1/2] Docs: for for/else clarify that return or raise also skip the else --- Doc/tutorial/controlflow.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Doc/tutorial/controlflow.rst b/Doc/tutorial/controlflow.rst index c97c65f7a3988e..5214d38f4d8e1e 100644 --- a/Doc/tutorial/controlflow.rst +++ b/Doc/tutorial/controlflow.rst @@ -209,8 +209,10 @@ after the loop finishes its final iteration, that is, if no break occurred. In a :keyword:`while` loop, it's executed after the loop's condition becomes false. -In either kind of loop, the :keyword:`!else` clause is **not** executed -if the loop was terminated by a :keyword:`break`. +In either kind of loop, the :keyword:`!else` clause is **not** executed if the +loop was terminated by a :keyword:`break`. Of course, other ways of ending the +loop early, such as a :keyword:`return` or a raised exception will also skip +execution of the :keyword:`else` clause. This is exemplified in the following :keyword:`!for` loop, which searches for prime numbers:: From b43e3410ed0262feebafbcd878f2f7fe5f94ada3 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Thu, 26 Sep 2024 10:17:08 -0400 Subject: [PATCH 2/2] Update Doc/tutorial/controlflow.rst Co-authored-by: Jelle Zijlstra --- Doc/tutorial/controlflow.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/tutorial/controlflow.rst b/Doc/tutorial/controlflow.rst index 5214d38f4d8e1e..fd765e58ff2485 100644 --- a/Doc/tutorial/controlflow.rst +++ b/Doc/tutorial/controlflow.rst @@ -211,7 +211,7 @@ In a :keyword:`while` loop, it's executed after the loop's condition becomes fal In either kind of loop, the :keyword:`!else` clause is **not** executed if the loop was terminated by a :keyword:`break`. Of course, other ways of ending the -loop early, such as a :keyword:`return` or a raised exception will also skip +loop early, such as a :keyword:`return` or a raised exception, will also skip execution of the :keyword:`else` clause. This is exemplified in the following :keyword:`!for` loop,