A while loop is used to repeatedly execute a block of code as long as a specified condition is True.
A while loop checks a condition before every iteration. If the condition is True, the loop body executes.
while condition:
statement
When the condition becomes False, the loop stops.
i = 1
while i <= 5:
print(i)
i = i + 1
count = 1
while count <= 3:
print("Hello")
count += 1
number = 1
while number <= 10:
print(number)
number += 1
number = 2
while number <= 10:
print(number)
number += 2
number = 1
while number <= 10:
print(number)
number += 2
number = 5
while number >= 1:
print(number)
number -= 1
Python allows an else block with a while loop. The else block executes when the loop ends normally.
i = 1
while i <= 3:
print(i)
i += 1
else:
print("Loop completed")
number = 1
while number <= 10:
if number % 2 == 0:
print(number)
number += 1
number = int(input("Enter a number: "))
while number <= 5:
print(number)
number += 1
If the user enters 2:
number = 1
total = 0
while number <= 5:
total += number
number += 1
print(total)
The loop calculates: 1 + 2 + 3 + 4 + 5 = 15.
number = 5
i = 1
while i <= 10:
print(number * i)
i += 1
name = "Python"
i = 0
while i < len(name):
print(name[i])
i += 1
names = ["Amit", "Rahul", "Priya"]
i = 0
while i < len(names):
print(names[i])
i += 1
An infinite loop occurs when the condition never becomes False.
i = 1
while i <= 5:
print(i)
The value of i never changes, so the condition remains True.
Updating the loop variable is one of the most important parts of a while loop.
i = 1
while i <= 5:
print(i)
i = i + 1
Here, i = i + 1 increases the value of i after every iteration.
The break statement immediately stops the loop.
i = 1
while i <= 10:
if i == 5:
break
print(i)
i += 1
The continue statement skips the remaining code of the current iteration and moves to the next iteration.
i = 0
while i < 5:
i += 1
if i == 3:
continue
print(i)
A while loop can be placed inside another while loop. This is called a nested while loop.
i = 1
while i <= 2:
j = 1
while j <= 3:
print(i, j)
j += 1
i += 1
password = ""
while password != "1234":
password = input("Enter password: ")
print("Login successful")
The loop continues until the correct password is entered.
choice = 1
while choice != 0:
print("1. Continue")
print("0. Exit")
choice = int(input("Enter choice: "))
print("Program ended")
This type of loop can be used to keep a program running until the user chooses an exit option.
number = 100
while number > 10:
print(number)
number -= 10
| For Loop | While Loop |
|---|---|
| Used to iterate over a sequence. | Runs while a condition is True. |
| Often used when the number of iterations is known. | Useful when the number of iterations depends on a condition. |
| Works naturally with lists, strings, ranges, etc. | Usually requires a condition and variable update. |
Like other Python control structures, while loops use indentation to define the loop body.
i = 1
while i <= 3:
print(i)
i += 1
i = 1
while i <= 3:
print(i)
The loop body must be properly indented.
Question: When does a while loop stop executing?