Introduction
In Python, loops are used to execute a block of code repeatedly as long as a given condition is satisfied.
Python provides two main types of loops:
-
forloop -
whileloop
In this blog, let's understand the while loop with simple examples.
What Is a While Loop?
A while loop is used to execute a block of code repeatedly as long as the given condition is True.
The condition is checked before executing the loop body. If the condition is False, the loop will not execute.
Basic Example
count = 0
while count < 5:
print(1, end=' ')
count = count + 1
Output:
1 1 1 1 1
Explanation:
- Initially,
count = 0. - The condition
count < 5is checked. - Since the condition is
True,1is printed. - Then
countis increased by 1. - This process continues until
countbecomes 5. - When
count = 5, the condition becomesFalse, and the loop stops.
Note: count = count + 1 can also be written as count += 1.
What Happens When We Change the While Loop?
Let's modify the same program and understand how the output changes.
Example 1: Change the Initial Value of Count to 1
count = 1
while count < 5:
print(1, end=' ')
count = count + 1
Output:
1 1 1 1
Explanation:
The initial value of count is 1. The loop executes while count is less than 5.
The values of count are 1, 2, 3, and 4. When it becomes 5, the condition becomes False.
So, the loop executes 4 times.
Example 2: Change the Initial Value of Count to 5
count = 5
while count < 5:
print(1, end=' ')
count = count + 1
Output:
(No output)
Explanation:
Initially, count = 5.
The condition is count < 5.
Since 5 is not less than 5, the condition is False.
Therefore, the loop body will not execute even once.
Example 3: Change the Increment Value from 1 to 2
count = 0
while count < 5:
print(1, end=' ')
count = count + 2
Output:
1 1 1
Explanation:
Here, the value of count increases by 2 instead of 1.
The values of count are:
- Initially: 0
- First iteration: 0, then becomes 2
- Second iteration: 2, then becomes 4
- Third iteration: 4, then becomes 6
When count becomes 6, the condition count < 5 becomes False.
So, the loop executes 3 times.
Example 4: Change the Increment to Decrement
count = 0
while count < 5:
print(1, end=' ')
count = count - 1
Output:
1 1 1 1 1 ...
Explanation:
Here, the value of count decreases by 1 in every iteration.
The values are:
0, -1, -2, -3, -4, -5, ...
The condition count < 5 remains True because these values are always less than 5.
Therefore, the loop never stops. This is called an infinite loop.
To avoid an infinite loop, make sure the variable is updated in a way that allows the condition to eventually become False.
Printing the Multiplication Table of 3
button = 1
while button <= 5:
print(button * 3, end=' ')
button = button + 1
print()
print(button)
Output:
3 6 9 12 15
6
Explanation:
- Initially,
button = 1. - The condition checks whether
button <= 5. -
button * 3prints the multiplication table of 3. - The value of
buttonincreases by 1 in every iteration. - When
buttonbecomes 6, the condition becomesFalse.
Why does the last print(button) display 6?
The variable is updated during the final iteration. After printing 15, button becomes 6.
Then the loop stops because 6 <= 5 is False.
The print(button) statement is outside the loop, so it prints the final value, which is 6.
Conclusion
The while loop is useful when we want to repeat a block of code based on a condition.
By changing the initial value, comparison operator, and increment or decrement value, we can control how many times the loop executes.
Understanding these small changes helps build a strong foundation in Python loops.
Top comments (0)