The while Loop
While loop is used to execute a set of statements as long as the provided condition is true, as shown in below example that i will be printed as long as i is less than 5
i = 1
while i < 5:
print(i)
i += 1
output
1
2
3
4
5
The break Statement
The break statement is used to stop the loop even if the while condition is true as shown in below example when i equals to 3 it will exit the loop while the i <5 or logical condition provided in while loop is evaluating true
i = 1
while i < 5:
print(i)
if i == 3:
break
i += 1
output
1
2
3
The continue Statement
The continue statement is used to stop the current iteration and continue with the next iteration as shown below in the example-
i = 0
while i < 5:
i += 1
if i == 3:
continue
print(i)
output
1
2
4
5
Note that in the above result, value 3 is missing.
Leave a Reply