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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions Doc/tutorial/controlflow.rst
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
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.


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.


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
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:`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
Loading