while
loop in Python works in just the same way as a while loop in any other programming language. Python While Loop
The while
loop which is basically a set of statements in the body of the loop which execute as many times as necessary whilst the loop’s condition is true
. Eventually, the statements in the loop will cause the condition to become false
, causing the loop to terminate.
while expression:
body of while
Example:
n = 1
while n < 5: # Declare loop with condition
print(n)
n = n+1
1
2
3
4
While loop with else Statement:
else
part is executed after the condition
in the while loop is executed.
n = 1
while n < 4:
print(n)
n += 1
else:
print("Loop ended")
1
2
3
Loop ended
While loop with break Statement:
break statement provides you the opportunity to exit out of a loop when an external condition is triggered. You’ll put the break statement within the block of code under your loop statement.
n = 1
while n < 10:
print(n)
if n == 5:
break
n += 1
BREAK statement applied as long as the condition is true
1
2
3
4
5
While loop with continue Statement:
continue
statement is opposite to that of break statement, instead of terminating the loop, it forces to execute the next iteration of the loop.
As the name suggests the continue statement forces the loop to continue or execute the next iteration. When the continue statement is executed in the loop, the code inside the loop following the continue statement will be skipped and the next iteration of the loop will begin.
n = 0
while n < 5:
n += 1
if n == 3:
continue
print(n)
1
2
4
5
for loop vs while loop
Here is a table that compares the for
loop and the while
loop in Python:
for loop | while loop |
---|---|
The for loop is used to iterate over a sequence (such as a list, tuple, or string) or other iterable object. | The while loop is used to repeatedly execute a block of code as long as a certain condition is true. |
The for loop has a definite iteration, that is, the loop variable takes on all the values in the sequence. | The while loop has an indefinite iteration, that is, the loop variable takes on values until the condition is no longer true. |
The for loop is best used when you know the number of iterations in advance. | The while loop is best used when you don’t know the number of iterations in advance. |
The for loop is less prone to errors since it is definite iteration. | The while loop is more prone to errors since it is indefinite iteration and if the condition is never met then the loop will run indefinitely. |
Here is an example of a for
loop:
fruits = ['apple', 'banana', 'cherry']
for x in fruits:
print(x)
apple
banana
cherry
Here is an example of a while
loop:
i = 1
while i < 6:
print(i)
i += 1
1
2
3
4
5
It’s important to note that while loop can be used to achieve the same functionality as a for loop but with a different approach and sometimes for loop can’t be used in certain situations where while loop can be used.
while
- The while
true
. - break statement provides you the opportunity to exit out of a loop.
- continue