FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-123786: Clarify ``else`` clause in loop statements by Gerardwx · Pull Request #123787 · python/cpython · GitHub

/ cpython Public
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .rst  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
10 changes: 4 additions & 6 deletions Doc/tutorial/controlflow.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,13 @@ arguments. In chapter :ref:`tut-structures`, we will discuss in more detail abo
The :keyword:`break` statement breaks out of the innermost enclosing
:keyword:`for` or :keyword:`while` loop.

A :keyword:`!for` or :keyword:`!while` loop can include an :keyword:`!else` clause.
The :keyword:`!break` statement may be paired with an :keyword:`!else` clause.
If the loop exits without executing the break, the else clause executes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
If the loop exits without executing the break, the else clause executes.
If the loop finishes without executing the break, the else clause executes.


In a :keyword:`for` loop, the :keyword:`!else` clause is executed
after the loop reaches its final iteration.
after the loop reaches its final iteration if no break occurred.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
after the loop reaches its final iteration if no break occurred.
after the loop finishes its final iteration, that is, if no break occurred.

I think this is clearer.


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`.
Comment on lines -176 to -177

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

We should keep this sentence. It underscores the semantics.

In a :keyword:`while` loop, it's executed after the loop's condition becomes false if no break occurred.

This is exemplified in the following :keyword:`!for` loop,
which searches for prime numbers::
Expand Down

Back | FazBrowse Home | New Git URL