Brandon Rozek

Photo of Brandon Rozek

PhD Student @ RPI studying Automated Reasoning in AI and Linux Enthusiast.

Do-While Loop in Other Languages

Published on

Updated on

Some languages like C, C++, and Java have a concept of a Do-While loop which normally look like the following:

do {
} while(condition);

This would ensure that your group of statements at least run once and then continue while the condition is still met. If you’re used to that pattern, then it can be annoying when you switch to another language like Python and find that it doesn’t exist. To replicate this behavior, its as simple as adding an extra variable.

first_run = True
while condition or first_run:
    first_run = False
Reply via Email Buy me a Coffee
Was this useful? Feel free to share: Hacker News Reddit Twitter

Published a response to this? :