Python Branching Statements

Python Branching Statements, Python Fundamentals, Flow Control Statements, three branching statements in Python break, continue, and return.

Branching Statements in Python

  • Branching statements in Python are used to change the normal flow of execution based on some condition.
  • Python provides three branching statements break, continue and return.
  • In Python pass also a branching statement, but it is a null statement.
  • The return branching statement is used to explicitly return from a method.
  • break branching statement is used to break the loop and transfer control to the line immediately outside of the loop.
  • continue branching statement is used to escape current execution and transfers control back to the start of the loop.
1. 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 behavior. the break will end the loop it is in and control flows to the statement immediately below the loop.

Example:

# break Statement Example
for i in range(1, 10):
if i == 4:
break
print(i)

Note: One to four numbers will execute in the above program

2. 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 a temporary break in a loop.

Example:

# continue Statement Example
for i in range(1, 10):
if i == 4:
continue
print(i)

Note: One to 10 numbers will execute and except 4th number in the above program

3. 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.

Example:

# return Statement Example
def func1():
x = 10
return x

def func2():
a = 123

# 10 is returned
print(func1())

# None is returned automatically
print(func2())


Python Step by Step Tutorial
Python Video Tutorial
Follow me on social media: