While Loop
A while loop is a type of loop that repeatedly executes a block of code as long as a condition is true.
Before each iteration, the condition is checked first.
If the condition is still true, the code inside the loop will run again.
If the condition becomes false, the loop stops.
How a While Loop Works
In general, a while loop consists of three main parts:
-
Initialization A starting value, usually defined before the loop begins.
-
Condition (termination condition) A logical expression checked before each iteration.
-
Update (increment or decrement) A change to the variable so the loop can eventually stop.
Execution Flow
The flow of a while loop looks like this:
- Check the condition
- If the condition is true → execute the loop body
- Update the variable
- Go back to step 1
This process repeats until the condition becomes false.
Example 1: Simple Counter
count = 0
while count < 5:
print(count)
count += 1
Explanation:
count = 0→ initializationcount < 5→ conditioncount += 1→ increment- The loop prints numbers from
0to4
Example 2: Loop Until User Input Is Correct
password = ""
while password != "secret":
password = input("Enter password: ")
print("Access granted")
Explanation:
- The loop continues while the password is incorrect
- The loop stops when the correct value is entered
- Useful when the number of attempts is unknown
Example 3: Decreasing Loop (Countdown)
timer = 5
while timer > 0:
print(timer)
timer -= 1
print("Time's up!")
Explanation:
timer = 5→ initializationtimer > 0→ conditiontimer -= 1→ decrement- The loop runs until
timerreaches0
Common Mistake: Infinite Loop
x = 0
while x < 3:
print(x)
This loop never stops because x is never updated.
Important Notes
- The condition must eventually become false
- If the variable is never updated, the loop may run forever
- This situation is called an infinite loop
When to Use a While Loop
A while loop is useful when:
- the number of repetitions is not known in advance
- the loop should run until a certain condition is met
In Short
- A while loop repeats code while a condition is true
- The condition is checked before each iteration
- Proper initialization and updates are required to stop the loop
Understanding the while loop helps you control program repetition safely and effectively.