While loops in python
While loops are a type of loops in python that can be used to iterate for as long as a preset condition is True.
while True:
print("True")
# This program iterates infinite times
- Using an increment
x = 0
while x < 50:
print(x)
x += 1
"""
This program prints the value of x and adds one
to the value of x from the previous iteration.
It repeats this iteration until the condition (x<50) returns False
"""
- using the break statement in a while loop
x = 0
while x < 50:
print(x)
x += 1
if x==25:
break
"""
This break statement is excecuted when the increment
makes the condition x == 25 True
"""
x = 0
while x < 50:
print(x)
x += 1
if x==25:
continue
"""
This continue statement is excecuted when the condition x == 25 is True.
The loop resumes after the condition x == 25 is skipped
"""