[Python-ideas] Immemorial desire for "do-while"-like construction
MRAB
python at mrabarnett.plus.com
Thu Jul 16 01:13:58 CEST 2009
Chris Rebert wrote:
> On Wed, Jul 15, 2009 at 3:50 PM, Jan Kaliszewski<zuo at chopin.edu.pl> wrote:
>> Hello,
>>
>> The issue has been coming back repeatedly:
>>
>> How we could elegantly and pythonic'ly avoid repeating ourselves if
>> we need the control flow structure that e.g. in PASCAL has the form:
>>
>> repeat
>> ...
>> until CONDITION
>>
>> In Python we must use while-loop in not very DRY (Don't Repeat
>> Yourself) manner:
>>
>> SOME
>> ACTIONS
>> HERE
>> ...
>> while CONDITION:
>> THE SAME
>> ACTIONS
>> AGAIN
>> ...
>
> You can fix that by just writing it as:
>
> while True:
> SOME
> ACTIONS
> HERE
> if not CONDITION: break
>
If we were going to add 'repeat' then it could be:
repeat:
SOME
ACTIONS
HERE
while CONDITION
If 'while' starts a loop it ends with a colon and the body of the loop
follows either on the same line or indented on the next lines. If
'while' ends a 'repeat' loop it doesn't end with a colon and there isn't
anything following on the same line or indented on the next lines.
More information about the Python-ideas
mailing list