Notice: While Javascript is not essential for this website, your interaction with the content will be limited. Please turn Javascript on for the full experience.

PEP 315 -- Enhanced While Loop

PEP: 315
Title: Enhanced While Loop
Author: Raymond Hettinger <python at>
Status: Rejected
Type: Standards Track
Content-Type: text/plain
Created: 25-Apr-2003
Python-Version: 2.5


    This PEP proposes adding an optional "do" clause to the beginning
    of the while loop to make loop code clearer and reduce errors
    caused by code duplication.


    Rejected; see

    This PEP has been deferred since 2006; see

    Subsequent efforts to revive the PEP in April 2009 did not
    meet with success because no syntax emerged that could
    compete with the following form:

        while True:
            <setup code>
            if not <condition>:
            <loop body>

    A syntax alternative to the one proposed in the PEP was found for
    a basic do-while loop but it gained little support because the
    condition was at the top:

        do ... while <cond>:
            <loop body>

    Users of the language are advised to use the while-True form with
    an inner if-break when a do-while loop would have been appropriate.


    It is often necessary for some code to be executed before each
    evaluation of the while loop condition.  This code is often
    duplicated outside the loop, as setup code that executes once
    before entering the loop:

        <setup code>
        while <condition>:
            <loop body>
            <setup code>

    The problem is that duplicated code can be a source of errors if
    one instance is changed but the other is not.  Also, the purpose
    of the second instance of the setup code is not clear because it
    comes at the end of the loop.

    It is possible to prevent code duplication by moving the loop
    condition into a helper function, or an if statement in the loop
    body.  However, separating the loop condition from the while
    keyword makes the behavior of the loop less clear:

        def helper(args):
            <setup code>
            return <condition>

        while helper(args):
            <loop body>

    This last form has the additional drawback of requiring the loop's
    else clause to be added to the body of the if statement, further
    obscuring the loop's behavior:

        while True:
            <setup code>
            if not <condition>: break
            <loop body>

    This PEP proposes to solve these problems by adding an optional
    clause to the while loop, which allows the setup code to be
    expressed in a natural way:

            <setup code>
        while <condition>:
            <loop body>

    This keeps the loop condition with the while keyword where it
    belongs, and does not require code to be duplicated.


    The syntax of the while statement

        while_stmt : "while" expression ":" suite
                     ["else" ":" suite]

    is extended as follows:

        while_stmt : ["do" ":" suite]
                     "while" expression ":" suite
                     ["else" ":" suite]

Semantics of break and continue

    In the do-while loop the break statement will behave the same as
    in the standard while loop: It will immediately terminate the loop
    without evaluating the loop condition or executing the else

    A continue statement in the do-while loop jumps to the while
    condition check.

    In general, when the while suite is empty (a pass statement),
    the do-while loop and break and continue statements should match
    the semantics of do-while in other languages.

    Likewise, when the do suite is empty, the do-while loop and
    break and continue statements should match behavior found
    in regular while loops.

Future Statement

    Because of the new keyword "do", the statement

        from __future__ import do_while

    will initially be required to use the do-while form.


    The first implementation of this PEP can compile the do-while loop
    as an infinite loop with a test that exits the loop.


    This document is placed in the public domain.