← Python Programming
Conditions and loops · 15 MIN

While loops and loop control

While loops and loop control

A while loop repeats while its condition is true. break exits the nearest loop and continue skips to the next iteration. Ensure the loop makes progress toward its stopping condition.

Try it yourself

Change the example, run it, and explain the result in your own words. Then create one variation of your own.

Key idea

Use the example as a starting point, not something to memorize. Practise, inspect the result and make small improvements.

Code example

attempts = 0
while attempts < 3:
    print("Attempt", attempts + 1)
    attempts += 1