Table of Contents
The if
statement is one of the most useful and fundamental conditional statements in many programming languages. We use the if
statement to check whether a condition is True or False and execute some code based on the result. We generally provide an alternative code, which is to be executed when the condition of the if
statement fails, using the else
statement.
Exit if
Statement in Python
This tutorial will demonstrate how to exit if
statement in Python.
Using the break
statement
The break
statement is used to break out of a loop in Python. We cannot directly use this with the if
statement. However, we can work around it using a loop.
We can use the while
loop and create an infinite loop by providing a True
condition. We put the if
statement within this loop and use the break
statement whenever we want to exit if
statement in Python. It will directly break
out of the loop and resume program execution.
We implement this in the code below.
1 2 3 4 5 6 7 8 9 10 11 |
a = 10 while True: if a > 5: print("Within if statement") break #exit if statement in Python print("If statement not exited") else: print("If statement still not exited") print("If statement exited!") |
Output:
If statement exited!
In the above example, we can observe that the if
statement is exited after encountering the break
statement.
Using the return
statement
We use the return
statement whenever we define a function to return some value. The function execution is exited automatically whenever the return
statement is encountered in the function definition. If the return
statement is missing then the function returns None
value by default.
We can put the if
statement within the function and use the return
statement to exit the function definition which automatically exits the if
statement.
See the code below.
1 2 3 4 5 6 7 8 9 10 11 12 |
def fun(): a = 10 if a > 5: print("Within if statement") return #exit if statement in Python print("If statement not exited") else: print("If statement still not exited") fun() print("If statement exited!") |
Output:
If statement exited!
Further reading:
Using the try
and except
statements
Python is efficient in exception handling. Exceptions are the errors that are encountered within the code that disrupt program execution. There are different types of exceptions in Python.
The try
and except
statements are used to work with exceptions in Python. We put code that may raise an exception within the try
block. We put an alternative code within the except
block. If the code in the try
block raises an exception, then the code of except
block is executed.
To raise manual exceptions we use the raise
statement. We can use a combination of these statements to exit if
statement in Python.
We can raise an exception in the if
block. Whenever this exception is raised, the program execution goes to the except
block. We can put the pass
keyword in the except
code block which executes nothing when encountered.
The above-mentioned logic is implemented below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
a = 10 try: if a > 5: print("Within if statement") raise #exit if statement in Python print("If statement not exited") else: print("If statement still not exited") except: pass print("If statement exited!") |
Output:
If statement exited!
Conclusion
To conclude, we discussed several methods to exit if
statement in Python. There are no direct methods available for the same, so we use different methods in which we shift control to some other part of the program.
In the first method, we use the break
statement to exit if
statement in Python. The if
statement is put within the while
loop and program execution comes out of the loop when the break
statement is encountered within the if
statement.
In the second method, we use the return
statement to exit if
statement in Python. We put the if
statement within a function and the return
statement is used to exit the function when it is encountered.
We use the try
and except
statements in the final method and use exception-handling to exit if
statement in Python. We manually raise an exception within the if
statement to exit the statement and resume program execution.