Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Const

while

The while statement repeatedly executes an indented suite while its condition is true. An optional else suite runs when the condition becomes false, but not when break exits the loop.

How it works

A while loop repeatedly evaluates its condition and executes its indented body whenever that condition is true. When the condition becomes false, the loop stops; an optional else suite then runs unless the body exited through break, while continue skips the remaining body and starts the next condition check.

Syntax

while assignment_expression:
    suite
[else:
    suite]

Parameters

NameTypeDescription
assignment_expressionexpressionExpression evaluated before each iteration; its truth value determines whether the suite runs.
suitesuiteIndented statements executed when the expression is true.
else suitesuiteOptional indented statements executed after normal termination.

Key points

  • while repeats its body for as long as its controlling expression is true.
  • The condition is reevaluated before each iteration, and a true result selects the body.
  • A false condition can stop the loop before the body executes even once.
  • After normal condition-based termination, an optional else suite executes before the loop finishes.
  • The required form consists of while, an assignment expression, a colon, and the loop suite.

Common mistakes

  • Using break prevents the loop’s else suite from running.
  • continue does not terminate the loop; it immediately begins another condition test.

Example: A while loop with else

count = 3
while count > 0:
    print(count)
    count -= 1
else:
    print("done")

Counts down while the condition remains true, then runs the else suite because the loop ends without break.

When to use it

Sentinel-controlled processing

Process input or stream data until a read operation produces a false value.

Bounded retries

Retry work while an attempt counter or readiness condition remains true.

State polling

Poll for a changing state until it reaches a desired condition.

Interview questions

QWhen is a while loop’s body executed?

The condition is tested before every iteration, including the first. If it is false initially, the body is skipped and the optional else suite runs.

Follow-up: What prevents the else suite from running? A break statement in the loop body terminates the loop without executing that suite.

QHow does a while loop terminate normally?

The loop repeatedly evaluates its condition. A true result executes the body, while a false result ends the loop and can trigger else.

Follow-up: What does continue do? It skips the remainder of the current body and returns to condition testing.

QWhat is the syntax of a while statement?

The syntax is while followed by an assignment expression, a colon, and an indented suite; an else clause is optional. The condition can be any expression whose truth value controls repetition.

Follow-up: Can the body execute zero times? Yes; a false condition on the initial test skips the body entirely.

  • if statement
  • for statement
  • break statement
  • continue statement
  • assignment expressions

Source: 8. Compound statements. Checked 2026-10-16.