Python while Loops Explained

A while loop lets you repeat code as long as a condition stays True.

This is useful when you do not know in advance how many times the loop should run. Python checks the condition before each run of the loop. When the condition becomes False, the loop stops.

Here is a quick example:

count = 1
while count <= 3:
    print(count)
    count += 1

Output:

1
2
3

Use a while loop when you want code to repeat as long as a condition stays True. Make sure something inside the loop changes, or the loop may never end.

What a while loop does

A while loop:

  • Repeats a block of code while a condition is True
  • Checks the condition before each loop run
  • Stops when the condition becomes False
  • Is useful when you do not know the exact number of repeats in advance

You can think of it like this:

  • “While this condition is true, keep going.”
  • “When it becomes false, stop.”

For example, if you want to keep asking for input until the user types "quit", a while loop is a good fit.

If you are still getting comfortable with conditions, see Python if statements explained.

Basic while loop syntax

A while loop has four important parts:

  • It starts with the keyword while
  • It needs a condition after while
  • The first line ends with a colon :
  • The loop body must be indented

Basic form:

while condition:
    # code to repeat

Example:

number = 1

while number < 4:
    print(number)
    number += 1

Output:

1
2
3

If you forget the colon, Python raises a syntax error. If you forget indentation, Python raises an indentation error. You can learn more in SyntaxError: missing colon and IndentationError: expected an indented block.

How execution works step by step

Python runs a while loop in this order:

  1. Python checks the condition.
  2. If the condition is True, it runs the indented code block.
  3. When that block finishes, Python checks the condition again.
  4. This repeats until the condition is False.

Look at this example:

count = 1

while count <= 3:
    print("Count is", count)
    count += 1

print("Loop finished")

Output:

Count is 1
Count is 2
Count is 3
Loop finished

What happens step by step:

  • count starts at 1
  • count <= 3 is True, so Python prints Count is 1
  • count becomes 2
  • count <= 3 is still True, so Python runs the loop again
  • After count becomes 4, the condition is False
  • Python exits the loop and moves to the next line

Simple counting example

A counting loop is one of the easiest ways to understand while.

count = 1

while count <= 5:
    print(count)
    count += 1

Output:

1
2
3
4
5

Important parts:

  • count = 1 creates the variable before the loop
  • count <= 5 is the condition
  • count += 1 updates the variable inside the loop

That last line is very important. Without it, count would stay 1, and the loop would never stop.

When to use while instead of for

Use a while loop when repetition depends on a changing condition.

Good uses for while:

  • Repeating until the user chooses to stop
  • Running code until a value reaches a target
  • Waiting for a condition to change

Use a for loop when you are going through a sequence such as:

  • A list
  • A string
  • A range of numbers

Example where while makes sense:

password = ""

while password != "python123":
    password = input("Enter the password: ")

print("Access granted")

The loop keeps running until the condition becomes False.

Example where for is usually better:

for letter in "cat":
    print(letter)

If you are comparing loop types, see Python for loops explained.

Avoiding infinite loops

An infinite loop is a loop that never ends because its condition never becomes False.

Example of an infinite loop:

count = 1

while count <= 3:
    print(count)

This keeps printing 1 forever because count never changes.

A fixed version:

count = 1

while count <= 3:
    print(count)
    count += 1

To avoid infinite loops:

  • Make sure the loop variable changes
  • Check that the condition can become False
  • Test with small examples first
  • Use print() statements if you need to see what is happening

Helpful debugging lines:

print(count)
print("condition still true")
help("while")

Common causes of loop problems:

  • Loop variable never changes
  • Condition is always True
  • Missing indentation after while
  • Using while for a task better suited to for

Using break and continue in while loops

Using break

break stops the loop immediately.

Example:

count = 1

while count <= 5:
    if count == 3:
        break
    print(count)
    count += 1

Output:

1
2

When count becomes 3, break ends the loop right away.

Using continue

continue skips the rest of the current loop run and moves to the next check.

Example:

count = 0

while count < 5:
    count += 1
    if count == 3:
        continue
    print(count)

Output:

1
2
4
5

When count is 3, Python skips print(count) for that loop run.

Use break and continue only when they make the loop easier to understand. For more examples, see Python break and continue statements.

The optional while-else block

A while loop can have an else block.

The else runs if the loop ends normally. It does not run if the loop stops because of break.

Example without break:

count = 1

while count <= 3:
    print(count)
    count += 1
else:
    print("Loop ended normally")

Output:

1
2
3
Loop ended normally

Example with break:

count = 1

while count <= 3:
    if count == 2:
        break
    print(count)
    count += 1
else:
    print("Loop ended normally")

Output:

1

The else block does not run here because break stopped the loop early.

Common beginner mistakes

Here are some of the most common while loop mistakes.

Forgetting to change the loop variable

Problem:

count = 1

while count <= 3:
    print(count)

Why it happens:

  • count never changes
  • The condition stays True

Fix:

count = 1

while count <= 3:
    print(count)
    count += 1

Using = instead of == in the condition

Problem:

count = 3

while count = 3:
    print(count)

This causes an error because = assigns a value, but == compares values.

Fix:

count = 3

while count == 3:
    print(count)
    break

Wrong indentation inside the loop

Problem:

count = 1

while count <= 3:
print(count)

This causes an indentation error because the loop body must be indented.

Fix:

count = 1

while count <= 3:
    print(count)

If indentation is still confusing, read Python indentation rules and why they matter.

Writing a condition that is always True

Problem:

while True:
    print("This keeps running")

This is not always wrong, but it will run forever unless you stop it with break or another exit condition.

Safer version:

count = 1

while count <= 3:
    print(count)
    count += 1

FAQ

What is a while loop in Python?

A while loop repeats code as long as its condition stays True.

When should I use a while loop?

Use it when the number of repeats depends on a condition, not a fixed sequence.

Why does my while loop never stop?

Usually because the condition never becomes False or the loop variable is not updated.

What is the difference between for and while in Python?

A for loop usually goes through items in a sequence. A while loop keeps going based on a condition.

Can I use break in a while loop?

Yes. break stops the loop immediately.

See also