Python-Ref > Basic usage > Looping > Skipping a step
 
 

<-^^
Klíčová slova
Moduly
Knihovní funkce

Skipping a step

How to prematurely jump to the next step in a loop.
In some cases it is desirable to skip one step in a loop, e.g. to avoid costly computation for an input value that is not interesting for us.
In this context the continue keyword is used. It jumps immediatelly to the next step of the loop, ignoring the rest of the code in the loop body.
Expand/Shrink
  1   # print the second and third power of a number, but only for odd numbers
  2   
  3   xs = [2, 4, 10, 13, 16, 10, 7, 5]
  4   
  5   for x in xs:
  6       if x % 2 == 0:
  7           continue
  8       print x, x*x, x*x*x
  9   
 10       
stdout:
13 169 2197
7 49 343
5 25 125
Doba běhu: 21.8 ms

Cvičení

  1. Rewrite the example above without the use of continue.