Hintor
Python Basics for Programmers

Repeating with while, break, and continue

Python for Developers/Python Basics for Programmers/

Repeating with while, break, and continue

Python

Lesson explanation

Repeating with while, break, and continue


🎯 Learning Objective

After this lesson, you will be able to:

  • Use while when repetition depends on changing state.
  • Update the controlling state so the loop can finish safely.
  • Stop immediately with break when the target is found.
  • Skip the rest of one iteration with continue.

Short Explanation

Use while when the program should repeat as long as a condition remains true:

Python
while attempts < max_attempts
Start Practice
:
...

Unlike a for loop, a while loop does not update its controlling state automatically. Your code must change the value used by the condition, or the loop may run forever.

break exits the entire loop immediately. Use it when continuing would be unnecessary, such as after finding a healthy server.

continue skips the remaining statements in the current iteration and returns to the loop condition. Before continuing, make sure any required index or counter update has already happened; otherwise the loop can repeat the same item forever.

Use for for a known numeric range or direct iteration over values. Use while when the stopping point depends on state that changes during execution.

💡 Tip: Before running a while loop, identify the condition, the state update, and the event that ends the loop.


Clean Code Examples

1. Repeat while state changes

The counter update eventually makes the condition false.

Python
attempt = 1

while attempt <= 3:
    print(f"Attempt {attempt}")
    attempt += 1

Output:

TEXT
Attempt 1
Attempt 2
Attempt 3

Without attempt += 1, this loop would not finish.

2. Stop when the target appears

break exits as soon as the healthy status is found.

Python
statuses = ["error", "error", "ready"]
index = 0

while index < len(statuses):
    status = statuses[index]
    index += 1

    if status == "ready":
        print(f"Ready on check {index}")
        break

Output:

TEXT
Ready on check 3

No later iteration is needed after success.

3. Skip one invalid reading

continue skips the print for the invalid value but keeps the loop running.

Python
readings = [18, -1, 22]
index = 0

while index < len(readings):
    reading = readings[index]
    index += 1

    if reading == -1:
        continue

    print(reading)

Output:

TEXT
18
22

The index is updated before continue, so the loop moves forward.

4. Combine continue and break

One condition skips unusable input; another ends processing.

Python
commands = ["", "status", "stop", "deploy"]
index = 0

while index < len(commands):
    command = commands[index]
    index += 1

    if not command:
        continue

    if command == "stop":
        break

    print(command)

Output:

TEXT
status

The empty command is skipped, and processing ends at stop.


NeedTool
Repeat while state remains validwhile
Exit the loop completelybreak
Skip only this iterationcontinue
Avoid an infinite loopUpdate the controlling state

⚠️ Common Mistakes

  1. Do not forget to update the value used by the loop condition.
  2. Do not use break when only one item should be skipped.
  3. Do not place continue before a required index update.
  4. Do not use while for a simple fixed range when for is clearer.