The rap against "while True:" loops

Bearophile bearophileHUGS at
Sun Oct 11 07:11:57 CEST 2009

Peter Billam:

> I remember in the structured-programming revolution the
>    loop { ... if whatever {break;} ... }
> idiom was The Recommended looping structure, because the code is
> more maintainable.

I think "break" was almost the antithesis of structured programming,
it was seen as the little (and a bit more well behaved) brother of
Too many "breaks" turn code almost into Spaghetti, that is the
opposite of structured programming.

> With a   while () {...}   idiom, you commit
> yourself to a bug-prone rewrite if you ever need to insert a
> statement before the first break-test,  and likewise with a
> repeat { ... } until ()   you commit yourself to a rewrite if
> you ever need to insert a statement after the last break-test.

I think while True:... is used often in Python because Python lacks
still a do-while (or repeat-until, but this is less nice, because the
condition is reversed compared to the one you use in a while-do loop)
Give me a do-while and a good amount of breaks&while True in my Python
code will be removed.


More information about the Python-list mailing list