+ 2

Cpp "do while" Python equivalent

What is the Python equivalent of the cpp "do while" loop ?

15th Jan 2021, 11:54 AM
Opixelum
Opixelum - avatar
5 Antworten
+ 4
♤♢☞ 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 ☜♢♤ no, without the negation it would be the equivalent of a do-until loop, implemented in visual basic and perl afaik. This variant maybe is a better semantic equivalent to do-while: while True: # some code if 《condition》: continue else: break If the condition holds, we continue iteration, else (=if 《condition》 is false) we break
15th Jan 2021, 8:10 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 6
The best equivalent imo is to use while loop with break. Note that the condition is negated. Cpp example: do { // some code } while 《condition》; Python equvalent: while True: # some code if not 《condition》: break
15th Jan 2021, 1:39 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 5
If the 'if' statement is the last one in the loop, the "continue" would be redundant.
16th Jan 2021, 12:21 AM
Sonic
Sonic - avatar
+ 2
python has a while loop not do while
15th Jan 2021, 12:04 PM
Slick
Slick - avatar
+ 2
I know Sonic, it was just to show the equivalence in another way and emphasize the need for the condition to be false in order to break
16th Jan 2021, 1:10 AM
Benjamin Jürgens
Benjamin Jürgens - avatar