In this article, we are going to focus on while loops used in python with suitable examples. The content of this article is shown below:
Introduction
While loop in Python is a primitive type of conditional loop by which repeated execution of a block of statements is done until an expression is true.
i=10
while i>6:
print(i)
i-=1
The above condition is satisfied until the value is greater than 6
The break statement
When we wan to forcefully stop the execution though the condition is true the break statement is used. To know more you can refer to this site.
i = 10
while i >2:
print(i)
if i == 6:
break
i -= 2
In the above code though the condition is greater than 2 but it stops execution at the time it reaches to 6
The continue Statement
This statement stops the current iteration and can start the next execution. To know more you can refer to this site.
i = 2
while i < 8:
i += 1
if i == 4:
continue
print(i)
The else Statement
Like to for loops we use else statement here also in order to run a block of code once the condition becomes false.
i = 2
while i < 8:
print(i)
i += 2
else:
print(" The value of i is not less than 8")