Examples of break and continue statements in Python programming

0

break and continue statements can alter the flow of a normal loop

break statement breaks the loop and go out from it.

continue statement breaks only the current round, but continue the loop

All Contributions

this exerice to practice on using the break statement

write a program that keep asking user to enter any thing in keyboard untill user enters the word 'quit'

while True:
    s = input('Enter something : ')
    if s == 'quit':
        break
    print('Length of the string is', len(s))
print('Done')

this exerice to practice on using the break statement

write a program that keep asking user to enter any thing in keyboard untill user enters the word 'quit'

while True:
    s = input('Enter something : ')
    if s == 'quit':
        break
    print('Length of the string is', len(s))
print('Done')

Continue inside nested-loop :

for i in range(4):
    for j in range(4):          
        if j==2:    
            continue
        print("The number is ",i,j);

Continue inside while-loop :

ignore printing the number 7

i = 0
while i <= 10:    
    if i == 7:
        i += 1
        continue  
    print("The Number is  :" , i)
    i += 1

output:

The Number is  : 0

The Number is  : 1

The Number is  : 2

The Number is  : 3

The Number is  : 4

The Number is  : 5

The Number is  : 6

The Number is  : 8

The Number is  : 9

The Number is  : 10

Continue inside for-loop :

loop for 10 rounds, when arriving the 7th round breaks the round to ignore printing the counter i

for i in range(10):    
    if i == 7:
        continue  
    print("The Number is :" , i)

total contributions (11)

New to examplegeek.com?

Join us