while loops
Module 1 — What is one while-loop?
Repeat a block of code as long as a condition is true.
Definition
One while-Small repeats an indented block of code as long as a condition has the value True delivers. Python evaluates the condition
before every run - it is already at the beginning False, the block is not executed once.
Syntax:
while <Bedingung>:
<Anweisungen>
For the loop to end, there must be in the block itself change something that causes the condition to occur at some point False sets. This is usually a counting variable.
Example 1.1 — a condition that eventually becomes False
Loading Python…Tests
Module 2 — Counting loops
Count up, count down, count in steps.
Count up and down
The typical counting loop has three components:
- a Date before the loop (
zahl = 1), - a Condition, which checks a final value (
zahl <= 5), - a Change the counting variable in the block (
zahl = zahl + 1).
If the third step is missing, the condition never changes — the loop runs endlessly.
In the first exercises you read the start or end value input() a (known from the “Variables” module); The check enters the entries automatically, you don't have to type anything in yourself. In the last two exercises you don't read anything, but rather
set the value yourself (e.g. B. n = 10) and lets the loop run over this self-selected area.
Example 2.1 — count up from 1 to 5
Loading Python…Tests
Example 2.2 — count down from 3 to 0
Loading Python…Tests
Module 3 — Accumulator pattern
Accumulate values in a variable.
Identification
Very often you need one in a loop second variable, the
outside the loop with 0 or "" is initialized and expanded in the block with each run. This pattern is called
Rechargeable battery — typical for sums, products or texts.
Example 3.1 — Sum from 1 to 5
Loading Python…Tests
Related pattern: until a limit is reached
A loop does not have to count in fixed increments. It can also as long as run until a variable exceeds a limit - here the same variable that checks the condition grows in the block (e.g. B. by doubling). That is no Accumulator (there is no second, accumulating variable), but a growing counting variable. You will need exactly this pattern later in the exercise “Double”.
Example 3.2 — double to &> 50
Loading Python…Tests
Module 4 — Abort & Continuous loop
When break? And how do you avoid it? while True-Fall?
The most common trap
A loop runs endlessly, if their condition never False will. Typical causes: You don't increment the count variable, you increment the wrong variable, or the condition can logically never occur. If your program depends, a while loop is usually to blame.
break — the emergency brake
Sometimes the termination condition is first right known in the block. Then you can come with me break exit the loop immediately. break always just ends that innermost Bow.
break is introduced here only as a concept. You deliberately do not need it for the six exercises in this experiment, but it is useful to have seen the technique.
Example 4.1 — break as a targeted termination
Loading Python…Tests
TipWriting strategy
Write this first Condition and think about what has to happen for them to False will. Only then the block. If you after 3 You won't see anything in the editor for a few seconds - don't panic, execution will stop automatically after a while.
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
Tutorials & Manuals
- Real Python — Python while loops: 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.