The continue statement is used to tell Python to skip the rest of the statements in the current loop block and to continue to the next iteration of the loop.
Example 6.6. Using the continue statement
#!/usr/bin/python # Filename : continue.py while True: s = raw_input('Enter something : ') if s == 'quit': break if len(s) < 3: continue print 'Sufficient length'
$ python continue.py Enter something : a Enter something : 12 Enter something : abc Sufficient length Enter something : quit
In this program, we accept input from the user, but we process them only if they are at least 3 characters long. So, we use the built-in len function which gives the length of the string. If the value returned by the len function is less than 3, then we skip the rest of the statements in the block using the continue statement, otherwise the rest of the statements in the loop are executed.
Note that the continue statement works with the for loop as well.