Tuesday, May 22, 2018

Break and Continue


continue : This statement jump to the loop without executing the next line of codes.

break : Break generally use for coming outside the loop without completing all the iterations.

Example:

item_list = ['apple','orange','banana','spam','grapes']

for item in item_list:
    if item == 'spam':
        continue
    print(item)
print("------------------------------")
for item in item_list:
    if item == 'spam':
        break
    print(item)

# Print all numbers divisible by 7 between 0 - 100 until you find a a number divisible by 11

for i in range(0, 100, 7):
    print(i)
    if i > 0 and i % 11 == 0:
        break


Output :

apple
orange
banana
grapes
------------------------------
apple
orange
banana
0
7
14
21
28
35
42
49
56
63
70
77

No comments:

Post a Comment

Program to check valid IP addresses