Python - Statement continue

Introduction

continue statement jumps to the top of the closest enclosing loop

The following example uses continue to skip odd numbers.

This code prints all even numbers less than 10 and greater than or equal to 0.

Remember, 0 means false and % is the remainder of division (modulus) operator.

This loop counts down to 0, skipping numbers that aren't multiples of 2-it prints 8 6 4 2 0:

Demo

x = 10 
while x: # from www .ja  v  a 2  s  .  c o  m
    x = x-1                        # Or, x -= 1 
    if x % 2 != 0: continue        # Odd? -- skip print 
    print(x, end=' ')

Result

The code might be clearer if the print were nested under the if:

Demo

x = 10 
while x: # from   ww w .ja va2 s. c  o  m
    x = x-1 
    if x % 2 == 0:                 # Even? -- print 
        print(x, end=' ')

Result