Python Branching Statements
Python Control Flow Statements
Branching Statements in Python
- Python provides three branching statements break, continue and return.
- In Python pass also a branching statement, but it is a null statement.
- Branching statements in Python are used to change the normal flow of execution based on some condition.
- 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 immediate outside of loop.
- continue branching statement is used to escape current execution and transfers control back to the start of the loop.
- Python branching statements break and continue statements have two forms: the labeled form and the unlabeled form.
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 .
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
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
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
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
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.