2003-05-02 18:53:32 -04:00
|
|
|
PEP: 315
|
|
|
|
Title: Enhanced While Loop
|
|
|
|
Version: $Revision$
|
|
|
|
Last-Modified: $Date$
|
2017-01-19 13:00:30 -05:00
|
|
|
Author: Raymond Hettinger <python@rcn.com>, W Isaac Carroll <icarroll@pobox.com>
|
2013-06-26 11:38:57 -04:00
|
|
|
Status: Rejected
|
2003-05-02 18:53:32 -04:00
|
|
|
Type: Standards Track
|
2017-01-19 13:00:30 -05:00
|
|
|
Content-Type: text/x-rst
|
2003-05-02 18:53:32 -04:00
|
|
|
Created: 25-Apr-2003
|
2005-06-18 02:21:36 -04:00
|
|
|
Python-Version: 2.5
|
2003-05-02 18:53:32 -04:00
|
|
|
Post-History:
|
|
|
|
|
|
|
|
|
|
|
|
Abstract
|
2017-01-19 13:00:30 -05:00
|
|
|
========
|
2003-05-02 18:53:32 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
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.
|
2003-05-02 18:53:32 -04:00
|
|
|
|
|
|
|
|
2006-02-24 17:43:04 -05:00
|
|
|
Notice
|
2017-01-19 13:00:30 -05:00
|
|
|
======
|
2006-02-24 17:43:04 -05:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
Rejected; see [1]_.
|
2013-06-26 11:38:57 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
This PEP has been deferred since 2006; see [2]_.
|
2006-02-24 17:43:04 -05:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
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::
|
2009-05-29 03:02:18 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
while True:
|
|
|
|
<setup code>
|
|
|
|
if not <condition>:
|
|
|
|
break
|
|
|
|
<loop body>
|
2009-05-29 03:02:18 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
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::
|
2013-06-27 04:48:28 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
do ... while <cond>:
|
|
|
|
<loop body>
|
2013-06-27 04:48:28 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
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.
|
2013-06-26 11:38:57 -04:00
|
|
|
|
2006-02-24 17:43:04 -05:00
|
|
|
|
2003-05-02 18:53:32 -04:00
|
|
|
Motivation
|
2017-01-19 13:00:30 -05:00
|
|
|
==========
|
2003-05-02 18:53:32 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
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::
|
2003-05-02 18:53:32 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
<setup code>
|
|
|
|
while <condition>:
|
|
|
|
<loop body>
|
2003-05-02 18:53:32 -04:00
|
|
|
<setup code>
|
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
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.
|
2003-05-02 18:53:32 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
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::
|
2003-05-02 18:53:32 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
def helper(args):
|
|
|
|
<setup code>
|
|
|
|
return <condition>
|
2003-05-02 18:53:32 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
while helper(args):
|
|
|
|
<loop body>
|
2003-05-02 18:53:32 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
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::
|
2003-05-02 18:53:32 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
while True:
|
|
|
|
<setup code>
|
|
|
|
if not <condition>: break
|
|
|
|
<loop body>
|
2003-05-02 18:53:32 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
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::
|
2003-05-02 18:53:32 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
do:
|
|
|
|
<setup code>
|
|
|
|
while <condition>:
|
|
|
|
<loop body>
|
2003-05-02 18:53:32 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
This keeps the loop condition with the while keyword where it
|
|
|
|
belongs, and does not require code to be duplicated.
|
2003-05-02 18:53:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
Syntax
|
2017-01-19 13:00:30 -05:00
|
|
|
======
|
2003-05-02 18:53:32 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
The syntax of the while statement::
|
2003-05-02 18:53:32 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
while_stmt : "while" expression ":" suite
|
|
|
|
["else" ":" suite]
|
2003-05-02 18:53:32 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
is extended as follows::
|
2003-05-02 18:53:32 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
while_stmt : ["do" ":" suite]
|
|
|
|
"while" expression ":" suite
|
|
|
|
["else" ":" suite]
|
2003-05-02 18:53:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
Semantics of break and continue
|
2017-01-19 13:00:30 -05:00
|
|
|
===============================
|
2003-05-02 18:53:32 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
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
|
|
|
|
clause.
|
2003-05-02 18:53:32 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
A continue statement in the do-while loop jumps to the while
|
|
|
|
condition check.
|
2005-06-18 02:21:36 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
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.
|
2005-06-18 02:21:36 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
Likewise, when the do suite is empty, the do-while loop and
|
|
|
|
break and continue statements should match behavior found
|
|
|
|
in regular while loops.
|
2003-05-02 18:53:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
Future Statement
|
2017-01-19 13:00:30 -05:00
|
|
|
================
|
2003-05-02 18:53:32 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
Because of the new keyword "do", the statement::
|
2003-05-02 18:53:32 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
from __future__ import do_while
|
2003-05-02 18:53:32 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
will initially be required to use the do-while form.
|
2003-05-02 18:53:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
Implementation
|
2017-01-19 13:00:30 -05:00
|
|
|
==============
|
|
|
|
|
|
|
|
The first implementation of this PEP can compile the do-while loop
|
|
|
|
as an infinite loop with a test that exits the loop.
|
2003-05-02 18:53:32 -04:00
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
|
|
|
|
References
|
|
|
|
==========
|
|
|
|
|
|
|
|
.. [1] Guido van Rossum, PEP 315: do-while
|
|
|
|
https://mail.python.org/pipermail/python-ideas/2013-June/021610.html
|
|
|
|
|
|
|
|
.. [2] Raymond Hettinger, release plan for 2.5 ?
|
|
|
|
https://mail.python.org/pipermail/python-dev/2006-February/060718.html
|
2003-05-02 18:53:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
Copyright
|
2017-01-19 13:00:30 -05:00
|
|
|
=========
|
|
|
|
|
|
|
|
This document is placed in the public domain.
|
2003-05-02 18:53:32 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-19 13:00:30 -05:00
|
|
|
..
|
|
|
|
Local Variables:
|
|
|
|
mode: indented-text
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
sentence-end-double-space: t
|
|
|
|
fill-column: 75
|
|
|
|
End:
|