Cpp "do while" Python equivalent | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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 Respostas
+ 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