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:
    ...

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

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

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

Output:

TEXT
status

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


NeedTool

⚠️ 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.
Start Practice

Python for Developers

Repeating with while, break, and continue

6 tasks
Lesson progress0 / 6 tasks completed

Tasks

Practice progress

Stop after recovery

View task

Complete the loop so processing stops immediately when the ready status is found.

Requirements

  • Keep the existing state updates and messages unchanged.
  • Fill only the missing loop-control keyword.

Expected output

Recovered on check 3
Code: PythonTask 1

Stop after recovery

Change the starter code or fill in the blanks, then run it before asking Hintor.

1
2
3
4
5
6
7
8
9
10
11

Assembled code

Preview of the code that will run

Generated code is hidden by default. Expand it only if you want to inspect what your current blank values produce.

Output / ConsolePreparing Python runtime...

0 / 6 tasks completed

Previous lessonBack to unitBack to track
status
=
=
"ready"
:
print(f"Ready on check {index}")
break
reading
=
=
-
1
:
continue
print(reading)
if not command:
continue
if command == "stop":
break
print(command)
Repeat while state remains validwhile
Exit the loop completelybreak
Skip only this iterationcontinue
Avoid an infinite loopUpdate the controlling state