Python continue Statement (With Examples)

Python continue statement: Here, we are going to learn about the continue statement in Python with examples. By IncludeHelp Last updated : December 18, 2023

Python continue statement

Like other programming languages, in Python, the continue statement is used to continue the loop executing by skipping rest of the statement written after the continue statement in a loop.

The continue statement does not terminate the loop execution, it skips the current iteration of the loop and continues the loop execution.

Syntax of the Python continue statement

Below is the syntax of the continue statement:

continue

# Within the conditon inside a loop
if condition:
    continue

Python continue Statement: Examples

To understand the use of the continue statement, practice these examples.

Example 1

Here, we are printing the serious of numbers from 1 to 10 and continuing the loop if counter’s value is equal to 7 (without printing it).

# python example of continue statement

counter = 1

# loop for 1 to 10
# terminate if counter == 7

while counter<=10:
	if counter == 7:
	    counter += 1 #increasing the counter
	    continue
	print(counter)
	counter += 1

print("outside of the loop")

Output

1 
2 
3 
4 
5 
6 
8 
9 
10
outside of the loop 

Example 2

Here, we are printing the characters of a string and continuing the loop to print of the characters if the character is not an alphabet.

# python example of continue statement

string = "IncludeHelp.Com"

# terminate the loop if 
# any character is not an alphabet 

for ch in string:
    if not((ch>='A' and ch<='Z') or (ch>='a' and ch<='z')):
        continue
    print(ch)

print("outside of the loop")

Output

I
n
c
l
u
d
e
H
e
l
p
C
o
m
outside of the loop

Python Tutorial


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.