Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-123786: Clarify else clause in loop statements #123787

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Gerardwx
Copy link
Contributor

@Gerardwx Gerardwx commented Sep 6, 2024

Copy link

cpython-cla-bot bot commented Sep 6, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app bedevere-app bot added awaiting review docs Documentation in the Doc dir skip news labels Sep 6, 2024
@nineteendo
Copy link
Contributor

Can you add the issue number to the title?

@Eclips4 Eclips4 changed the title For else gh-123786: Clarify else clause in loop statements Sep 6, 2024
Comment on lines -176 to -177
In either kind of loop, the :keyword:`!else` clause is **not** executed
if the loop was terminated by a :keyword:`break`.
Copy link
Member

Choose a reason for hiding this comment

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

We should keep this sentence. It underscores the semantics.


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
Member

Choose a reason for hiding this comment

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

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.

@@ -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
Member

Choose a reason for hiding this comment

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

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.

@nedbat
Copy link
Member

nedbat commented Sep 11, 2024

I've made a larger change to this section, including these changes, in #123946

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review docs Documentation in the Doc dir skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants