In programming, loops are used to repeat a block of code. For example, if we want to show a message 100 times, then we can use a loop. It's just a simple example, we can achieve much more with loops.
In the previous tutorial, we learned about Python for loop. Here, we are going to learn about # program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 10 loops.
Python # program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 10 loop is used to run a specific code until a certain condition is met.
The syntax of # program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 10 loop is:
Here,
- A # program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 10 loop evaluates the # program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 14
- If the # program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 14 evaluates to # program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 16, the code inside the # program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 10 loop is executed.
- # program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 14 is evaluated again.
- This process continues until the condition is # program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 19.
- When # program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 14 evaluates to # program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 19, the loop stops.
Flowchart for Python While Loop
Example: Python while Loop
# program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 1Output
1 2 3 4 5Here's how the program works:
VariableCondition: 1
2
3
4
52Action1
2
3
4
53
1
2
3
4
54# program to display numbers from 1 to 5
# initialize the variable
i = 1
n = 5
# while loop from i = 1 to 5
while i <= n:
print(i)
i = i + 161
2
3
4
56 is printed. 1
2
3
4
57 is increased to 2.1
2
3
4
58
1
2
3
4
54# program to display numbers from 1 to 5
# initialize the variable
i = 1
n = 5
# while loop from i = 1 to 5
while i <= n:
print(i)
i = i + 16current_level = 0
final_level = 5
game_completed = True
while current_level <= final_level:
if game_completed:
print('You have passed level', current_level)
current_level += 1
print('Level Ends')1 is printed. 1
2
3
4
57 is increased to 3.current_level = 0
final_level = 5
game_completed = True
while current_level <= final_level:
if game_completed:
print('You have passed level', current_level)
current_level += 1
print('Level Ends')3
1
2
3
4
54# program to display numbers from 1 to 5
# initialize the variable
i = 1
n = 5
# while loop from i = 1 to 5
while i <= n:
print(i)
i = i + 16current_level = 0
final_level = 5
game_completed = True
while current_level <= final_level:
if game_completed:
print('You have passed level', current_level)
current_level += 1
print('Level Ends')6 is printed. 1
2
3
4
57 is increased to 4.current_level = 0
final_level = 5
game_completed = True
while current_level <= final_level:
if game_completed:
print('You have passed level', current_level)
current_level += 1
print('Level Ends')8
1
2
3
4
54# program to display numbers from 1 to 5
# initialize the variable
i = 1
n = 5
# while loop from i = 1 to 5
while i <= n:
print(i)
i = i + 16You have passed level 0
You have passed level 1
You have passed level 2
You have passed level 3
You have passed level 4
You have passed level 5
Level Ends1 is printed. 1
2
3
4
57 is increased to 5.You have passed level 0
You have passed level 1
You have passed level 2
You have passed level 3
You have passed level 4
You have passed level 5
Level Ends3
1
2
3
4
54# program to display numbers from 1 to 5
# initialize the variable
i = 1
n = 5
# while loop from i = 1 to 5
while i <= n:
print(i)
i = i + 16You have passed level 0
You have passed level 1
You have passed level 2
You have passed level 3
You have passed level 4
You have passed level 5
Level Ends6 is printed. 1
2
3
4
57 is increased to 6.You have passed level 0
You have passed level 1
You have passed level 2
You have passed level 3
You have passed level 4
You have passed level 5
Level Ends8
1
2
3
4
54# program to display numbers from 1 to 5
# initialize the variable
i = 1
n = 5
# while loop from i = 1 to 5
while i <= n:
print(i)
i = i + 19The loop is terminated.
Example 2: Python while Loop to Display Game Level
current_level = 0 final_level = 5 game_completed = True while current_level <= final_level: if game_completed: print('You have passed level', current_level) current_level += 1 print('Level Ends')Output
You have passed level 0 You have passed level 1 You have passed level 2 You have passed level 3 You have passed level 4 You have passed level 5 Level EndsIn the above example, we have used the # program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 10 loop to check the current level and display it on the console.
Infinite while Loop in Python
If the # program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 14 of a loop is always # program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 16, the loop runs for infinite times (until the memory is full). For example,
# infinite while loop while True: # body of the loopIn the above example, the # program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 14 is always # program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 16. Hence, the loop body will run for infinite times.
Python While loop with else
A # program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 10 loop can have an optional # infinite while loop while True: # body of the loop7 block as well.
The # infinite while loop while True: # body of the loop7 part is executed after the # program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 14 in the while loop evaluates to # program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 19. For example,
counter = 0 while counter < 3: print('Inside loop') counter = counter + 1 else: print('Inside else')Output
Inside loop Inside loop Inside loop Inside elseHere, we have used the counter variable to print the counter = 0 while counter < 3: print('Inside loop') counter = counter + 1 else: print('Inside else')1 string three times.
On the fourth iteration, the condition in # program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 10 becomes # program to display numbers from 1 to 5 # initialize the variable i = 1 n = 5 # while loop from i = 1 to 5 while i <= n: print(i) i = i + 19. Hence, the # infinite while loop while True: # body of the loop7 part is executed.