Loading, please wait...

The break Statement

With the help up of break statement you can stop the loop even if the while condition is true:

Example

Exit the loop when i is 4:

i = 1
while i < 6:
  print(i)
  if i == 4:
    break
  i += 1

output

C:\Users\My Name>python demo_while_break.py
1
2
3