break while loop python code example

Example 1: continue python

The continue statement in Python returns the control to the beginning of the 
while loop. The continue statement rejects all the remaining statements 
in the current iteration of the loop and moves the control back to the top of 
the loop.

The continue statement can be used in both while and for loops.

Example 2: python get out of loop

while True:
  print('I run!')
  break
print('Not in loop!')

Example 3: python break for loop

#in Python, break statements can be used to break out of a loop
for x in range(5):
    print(x * 2)
    if x > 3:
        break

Example 4: python exit loop iteration

alphabet = ['a' , 'b' , 'c' , 'd' ]
for letter in alphabet:
  if letter == 'b' :
    continue
    #continues to next iteration
  print( letter )
for letter in alphabet:
  if letter == 'b' :
    break
    #terminates current loop
  print( letter )
for letter in alphabet:
  if letter == 'b' :
    pass
    #does nothing
  print( letter )

Example 5: break while loop python

1n = 5
 2while n > 0:
 3    n -= 1
 4    if n == 2:
 5        break
 6    print(n)
 7print('Loop ended.')

Example 6: break function python

if( x > 3):
  print('X is greater than 3')
else:
  break

Tags:

Misc Example