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. In other words, the loop will not terminate immediately but it will continue on with the next iteration. This is in contrast with the break statement which will terminate the loop completely.

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 because at the point 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
!
>>>
Geek University 2022