while loop break python code example
Example 1: python break for loop
for x in range(5):
print(x * 2)
if x > 3:
break
Example 2: python while continue
while True:
print('Who are you?')
name = input()
if name != 'Joe':
continue
print('Hello, Joe. What is the password? (It is a fish.)')
password = input()
if password == 'swordfish':
break
print('Access granted.')
Example 3: python exit loop iteration
alphabet = ['a' , 'b' , 'c' , 'd' ]
for letter in alphabet:
if letter == 'b' :
continue
print( letter )
for letter in alphabet:
if letter == 'b' :
break
print( letter )
for letter in alphabet:
if letter == 'b' :
pass
print( letter )
Example 4: break while loop python
1n = 5
2while n > 0:
3 n -= 1
4 if n == 2:
5 break
6 print(n)
7print('Loop ended.')