Geek University

The continue statement

The continue statement in Python is used to skip the rest of the code inside a loop for the current iteration only. What this means is that, unlike with the break statement, the loop does not terminate but continues on with the next iteration. The continue statement is usually used inside an if statement that defines the condition for not executing the statements inside the loop.

Here is an example:

for i in range(1,11):
    if i==5:
        continue
    print (i)

The output:

>>> 
1
2
3
4
6
7
8
9
10
>>>

The example above prints all numbers from 1 to 10 except the number 5. This is so because when the variable i becomes equal to 5, the if statement will be executed and the continue statement inside of it will force the program to skip the print statement.

Here’s another example:

word = input('Type a word: ')

for i in word:
    if (i == 'a' or i=='e' or i=='o' or i=='u' or i=='i'):
        continue
    print (i)

The program above will print the word you enter without the vowels:

>>> 
Type a word: Hello world!
H
l
l

w
r
l
d
!
>>>

Subscribe to our Newsletter

Like us on Facebook

Share on Twitter

top
Developed by Signum Soft