Python : continue, break and pass statement

Hello Friends,

Today in this article we are going to discuss about continue, break and pass statement in Python

Python break statement

The break statement terminates the loop containing it.

If the break statement is inside a nested loop (loop inside another loop), the break statement will terminate the loop.

Syntax

break

Flow chart

Flowchart – 1 Break statement

Example 1 – Break

# Program to show how break statement works
for icounter in range(10):
    if icounter == (5):
        break
# Here it terminates the for loop
    print(icounter)
# It will print only 0 to 4 number
------------------------------------------------
Output
0
1
2
3
4

Python continue statement

The continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop does not terminate but continues on with the next iteration.

Syntax

continue

Flowchart

Flowchart - 2 Continue statement
Flowchart – 2 Continue statement

Example – 2 Continue

# Program to show how continue statement works
for icounter in range(10):
    if icounter == (5):
        continue
# Here it will skip 5
    print(icounter)
--------------------------------------------------
Output
0
1
2
3
4
6
7
8
9

Python pass statement

The pass statement is used as a placeholder for future code.

When the pass statement is executed, nothing happens, but you avoid getting an error when empty code is not allowed.

Empty code is not allowed in loops, function definitions, class definitions, or in if statements.

Syntax

pass

Example – 3 Pass

# Program to show how pass statement works
for icounter in range(10):
    pass
# Here for loop is empty but pass statement does not gives an error 

Program without pass statement

# Program to without pass statement
for icounter in range(10):
--------------------------------------
error - 
IndentationError: expected an indented block after 'for' statement on line 2

Thank you

Have a nice day 🙂

You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: