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:
x = 10 while x: # from w ww .j a va2s.c o m x = x-1 # Or, x -= 1 if x % 2 != 0: continue # Odd? -- skip print print(x, end=' ')
The code might be clearer if the print were nested under the if:
x = 10 while x: # w w w.ja va 2 s . c o m x = x-1 if x % 2 == 0: # Even? -- print print(x, end=' ')