A Byte of Python

The continue statement

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.

Using the continue statement

Example 6.5. Using the continue statement

# Filename: continue.py

while True:
	s = raw_input('Enter something : ')
	if s == 'quit':
	if len(s) < 3:
	print 'Input is of sufficient length'
	# Do other kinds of processing here...


$ python continue.py
Enter something : a
Enter something : 12
Enter something : abc
Input is of sufficient length
Enter something : quit

How It Works

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 to get the length and if the length is less than 3, we skip the rest of the statements in the block by using the continue statement. Otherwise, the rest of the statements in the loop are executed and we can do any kind of processing we want to do here.

Note that the continue statement works with the for loop as well.