Continue

Another useful keyword for loops is the continue keyword. When we use continue, the rest of the loop after the keyword is skipped for that iteration. 

Example

j = 0

for i in range(5):

    j = j + 2

    print('\ni =', i, ', j = ', j)

    if j == 6:

        continue

    print('I will be skipped if j = 6')


Output

i =  0 , j =  2

I will be skipped if j = 6


i =  1 , j =  4

I will be skipped if j = 6


i =  2 , j =  6


i =  3 , j =  8

I will be skipped if j = 6


i =  4 , j =  10

I will be skipped if j = 6


When j = 6, the line after the continue keyword is not printed. Other than that, everything runs normally.

No comments:

Post a Comment