Python While Loop
In this post we are going to explain while loop.
When a while loop is encountered, expr is first evaluated in Boolean context. If it is true, the loop body is executed. Then expr is checked again, and if still true, the body is executed again. This continues until expr becomes false, at which point program execution proceeds to the first statement beyond the loop body. For example:
i = 0
while i <10:
print(i)
i+=1
Output: 0
1
2
3
4
5
6
7
8
9
We can also use to while loop to get items from lists.
li = ['Orange','Apple','Banana']
i = 0
While i <len(li):
print(li[i])
Output: Orange
Apple
Banana
Infinite loop.
While True:
print(x)
Output: x
x
x
.
.