do-while loops
Module 1 — What is do … while?
A loop that at least once running.
Basic idea
In other languages like C or Java, there are two types of loops — head-driven and foot-driven. The post-test Variant do { … } while (…) checks the condition first at the end each run. Consequence: The block runs guaranteed at least once — even if the condition is false from the start.
This is useful if you first want to do something (e.g. read an input, display a menu, start a test) and only afterwards decides whether to continue.
ImportantPython doesn't have its own do … while
Python only knows while and for. There is no post-test loop. The idiomatic pattern is one Infinite loop with explicit exit:
while True:
<Anweisungen> # läuft mindestens einmal
if <Abbruch>:
break # Ausstieg am Ende des Blocks
The test is over at the end of the block. This means that the block is always fully executed once before deciding whether there is another iteration.
Example 1.1 — a block that runs exactly once
Loading Python…Tests
Module 2 — The Python pattern in detail
while True + break at the end of the block.
Construction step by step
- Paper
while True:— the loop starts without a preliminary check. - Put the actual work in the loop body: produce output, perform calculations and update counter variables.
- Am End of the block:
if <Abbruchbedingung>: break.
So the logic corresponds to a classic one do { … } while (!Abbruch); — except that Python formulates the “flipped” condition as a termination condition.
Example 2.1 — count until a final value is reached
Loading Python…Tests
Variant: collect points until a limit is exceeded
Often you want a count variable in the block change and then check at the end whether you have reached a threshold. This is one of the most typical do-while situations.
Example 2.2 — Collect points up to ≥ 3
Loading Python…Tests
Module 3 — Input and validation loops
The classic: “Keep asking questions until you get the answer.”
Why do-while here?
During an input check you must first ask for a value and
then assess whether it is valid. A head-controlled one
while-loop would otherwise have nothing to check.
The examples below use fixed values so you can test them directly with Run. In the programming tasks you read some of the values input() (the test automatically feeds in the entries, input() was introduced in the “Variables” module) and you define some of them yourself in the code - depending on the exercise, this is what it says.
Example 3.1 — Check word until it "okay" is
Loading Python…Tests
Example 3.2 — Password test
Loading Python…Tests
Module 4 — Common pitfalls
Endless loop, forgotten break, wrong position of the test.
Trap 1: This break missing
Without break is while True: a real endless loop. In Pyodide, execution stops automatically after a while — in a normal Python script, the program hangs permanently.
Trap 2: Exam at the wrong end
Do you set that? if … break before your actual work, you have a head-controlled loop again - the block then runs maybe not at all. This is the break with the do-while idea.
while True:
if abbruch:
break # ← prüfen zuerst → kopfgesteuert
arbeite()
while True:
arbeite()
if abbruch:
break # ← prüfen am Ende → fußgesteuert (do-while)
Trap 3: Condition is never changed in the block
The do-while logic is of no use if the value relevant to the termination condition never changes. Make sure your block adjusts the count variable, word, password, etc. so that at some point break is triggered.
Example 4.1 - incorrect position of the test (for comparison)
Loading Python…Tests
Sources & further literature
Where the knowledge imparted here is officially documented.
Official Python documentation
- Python Language Reference — The
whilestatement: docs.python.org/3/reference/compound_stmts.html#the-while-statement - Python tutorial — 4. More Control Flow Tools: docs.python.org/3/tutorial/controlflow.html
- Wikipedia — Do while loop (Concept and language comparison): en.wikipedia.org/wiki/Do while loop
Tutorials & Manuals
- Real Python — Python while loops (Section to
while True+break): realpython.com/python-while-loop - Theis, Thomas: Getting started with Python. 7. Edition, Rheinwerk Verlag, 2023 - Chapter on grinding.
- Klein, Bernd: Introduction to Python 3. 4. Edition, Hanser Verlag, 2021 — “Grinding” section.