Tutorials

Python 3 Tutorial

All Tutorials Learning Tools Login

Python 3 Tutorial · Chapter 17 of 22

Python Do-While Style

Run at least once using while True and break.

Python has no do-while keyword. The common pattern is while True with a break condition at the end.

Use this when the body must run once before checking the stop rule.

Do-while equivalent Try it Yourself »
i = 1
while True:
    print(i)
    i += 1
    if i > 5:
        break

Classroom Tips

  • Always include a clear break condition.
  • Prefer a normal while when the check can happen first.