Python break and continue statements

The break and continue statements are called Decision-Making Statements in Python. Let us understand them one by one:

break statement in Python

In the break statement, the current loop is terminated, and the execution of the program is aborted. After termination, the control reaches the next line after the loop.

Syntax

break;

Example – break statement

Let us see an example of the break statement in Python:

Demo22.py

# break statement in Python
# Code by studyopedia

mystr = "Studyopedia"

print("String = ",mystr)

for i in mystr:
    if i == 'p':
        break;
    print(i)

The output is as follows:

String =  Studyopedia
S
t
u
d
y
o

continue statement in Python

The continue statement in Python transfers control to the conditional expression and jumps to the next iteration of the loop.

Syntax

continue;

Example – continue statement

Let us see an example of the continue statement in Python:

Demo23.py

# continue statement in Python
# Code by studyopedia

mystr = "Studyopedia"

print("String = ",mystr)

for i in mystr:
    if(i=='p'):
        continue;
    print(i)

The output is as follows:

String =  Studyopedia
S
t
u
d
y
o
e
d
i
a

If you liked the tutorial, spread the word and share the link and our website, Studyopedia, with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

Python Comments
Python Modules
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment