while loop

A loop that keeps repeating as long as a condition stays true.

A while loop repeats its body over and over as long as a condition is True, checking the condition before each round. Use it when you do not know in advance how many times to repeat — waiting for valid input, retrying, or running until a total is reached. Something inside must eventually make the condition false, or the loop never ends.

Example
count = 0
while count < 3:        # check before each round
    print("tick", count)
    count += 1          # move toward the stop condition
Output
tick 0
tick 1
tick 2

Where this shows up in real Python

While loops drive menus, retry-until-success logic, and any process that runs until a condition changes rather than a fixed number of times.

Common while-loop tools

  • while condition: — repeat while it stays True
  • break — leave the loop immediately
  • continue — skip to the next check
  • while True: … break — loop until you decide to stop

Related lessons

Related terms