the_drow
the_drow

Reputation: 19181

Why isn't there a do while flow control statement in python?

Is there a good reason why there isn't a do while flow control statement in python?

Why do people have to write while and break explicitly?

Upvotes: 16

Views: 2675

Answers (4)

Catharsis
Catharsis

Reputation: 476

Because then you would have two ways to do something.

Upvotes: 3

user97370
user97370

Reputation:

Python adds features only when they significantly simplify some code.

while True:
    ...
    if not cond: break

is not less simple than a do-while loop, for which there is no obvious natural python syntax anyway.

do:
    ...
    while cond

(Looks weird)

or this?

do:
    ...
while cond

(The while looks like a regular while statement)

Upvotes: 2

Pär Wieslander
Pär Wieslander

Reputation: 28934

It has been proposed in PEP 315 but hasn't been implemented because nobody has come up with a syntax that's clearer than the while True with an inner if-break.

Upvotes: 11

JesperE
JesperE

Reputation: 64414

Probably because Guido didn't think it was necessary. There are a bunch of different flow-control statements you could support, but most of them are variants of each other. Frankly, I've found the do-while statement to be one of the less useful ones.

Upvotes: 10

Related Questions