Previous
Next
Share This Page On :

Python Branching Statements

Python Control Flow Statements

Branching Statements in Python

In this tutorial you can learn about Python Branching Statements like break Statement, continue Statement and return Statement. This tutorial is also example for branching statements in Python.

Branching Statements in Python

Python programming language provides following types of decision making or branching statements in Python . Python programming language provided decision making statements or branching statements as follows .

if statement

An if statement consists of a boolean expression followed by one or more statements .

if else statement

An if statement can be followed by an optional else statement , which executes when the boolean expression is false .

if elif else statement (nested if statement)

You can use one if elif else statements as nested if statements in other programming languages.

What are the Branching Statements in Python ?

Python provides 3 branching statements named break, continue and return. Branching statements are used to change the normal flow of execution based on some condition. Some branching statements are used in looping statements and others used to change the flow of execution of the program.

What are the statements used for branching in Python ?

In Python , Branching statements allow the flow of execution to jump to a different part of the program. The common branching statements used within other control structures are break , continue , return and goto .

What are Jump Statements in Python ?

In Python, Jump statements are used to unconditionally transfer program control from one point to elsewhere in the program. Jump statements are primarily used to interrupt loop instantly. Python supports three jump statements break, continue, and return. In Python pass also a branching statement, but it is a null statement.

How to Use break Statement in Python ?

The break statement is used to break out of a loop. It is used inside for and while loops to alter the normal behaviour. break will end the loop it is in and control flows to the statement immediately below the loop.

Python break Statement Example

# BREAK STATEMENT EXAMPLE
for i in range(1, 11):
    if i == 7:
        break
    print(i)
 
 
print("Loop terminated")

How to Use continue Statement in Python ?

The continue keyword is used to end the current iteration in a for loop or a while loop, and continues to the next iteration. 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.

Python continue Statement Example

# CONTINUE STATEMENT EXAMPLE
for i in range(1, 11):
    if i == 7:
        continue
    print(i)

How to Use return Statement in Python ?

The return statement is used inside a function to exit it and return a value. If you won't return a value explicitly, None is returned automatically in Python.

Python return Statement Example

# RETURN KEYWORD EXAMPLE
def func1():
    x = 7
    return x
 
 
def func2():
    a = 10
 
 
# 7 is returned
print(func1())
 
# None is returned automatically
print(func2())

How to Use pass Statement in Python ?

The pass is a null statement in Python. Nothing happens when it is executed. It is used as a placeholder. Empty code is not allowed in loops, function definitions, class definitions, or in if statements. If we have a function that is not implemented yet, but we want to implement it in the future, it will throw IndentationError exception. Instead of this, we construct a blank body with the pass statement. We can do the same thing in an empty class as well.

Python pass Statement Example

# PASS KEYWORD EXAMPLE
def func1(args):
    pass
 
 
class Person:
    pass
 
 
print("Successfully Executed")

Python Branching Statements Summary

Python provides 3 branching statement named break, continue and return. Branching statements are used to change the normal flow of execution based on some condition. The Python return statement is used to explicitly return from a method.

More Related Tutorials

Previous
Next