Table of Contents
Using break
Statement
Use the break
statement to exit if
statement in Python.
1 2 3 4 5 6 7 |
for num in range(6): print(num) if num == 3: break print('Loop Exited') |
1 2 3 4 5 6 7 |
0 1 2 3 Loop Exited |
The break
statement is used to terminate the loop prematurely. This statement is commonly utilized in loops to exit from them if a particular condition was met.
In the above example, we used a for
loop to iterate over a sequence of numbers that were created using a range() function. Here, the range()
created a sequence from 0
to 5
.
In each iteration, we used the print()
function to print the current number (num
) followed by an if
statement to determine if the num
is equal to 3
.
If it was, the loop was terminated due to a break
statement. Outside the loop, we used another print
statement to display a message on the console saying the loop was terminated.
What about the user input? Can we still exit the if
statement in Python if we are supposed to exit the loop based on the user’s input? See the following example.
1 2 3 4 5 6 7 8 |
for num in range(6): print(num) choice = input("Do you want to exit (Y for Yes, N for No): ") if choice == 'y' or choice == 'Y': break print('Loop Exited') |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
0 Do you want to exit (Y for Yes, N for No): n 1 Do you want to exit (Y for Yes, N for No): n 2 Do you want to exit (Y for Yes, N for No): n 3 Do you want to exit (Y for Yes, N for No): n 4 Do you want to exit (Y for Yes, N for No): y Loop Exited |
This example resembles the previous one; however, we used the input()
function to prompt the user and take input from him. Then, the if
statement was used with an or
operator to check multiple conditions; in our case, we checked if the user’s input was equal to y
or Y
.
The if
block would be executed if at least one conditional expression was fulfilled; otherwise, the loop would continue with its next iteration.
Using return
Statement
Use the return
statement to exit if
statement in Python.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
def evenOdd(num): # if num%2 == 0, then num is even if num % 2 == 0: return True # if num%2 == 1, then num is odd else: return False num = 2 if(evenOdd(num)): print(num, "num is even") else: print(num, "num is odd") |
1 2 3 |
2 num is even |
The primary use of the return
statement is to return one or multiple values from a function; however, we can also use it to exit from a loop if a certain condition is satisfied.
In the above example, we used the def
keyword to define the evenOdd()
function. Inside this function, we wrote logic to check if number is even or odd.
Outside this function, we invoked it as evenOdd()
which returned "2 num is even" as it checked if statement and returned True
as num is even.
Similarly, we can exit based on the user’s input; see the following example:
1 2 3 4 5 6 7 8 9 10 |
def fun(): for num in range(6): print(num) choice = input("Do you want to exit (Y for Yes, N for No): ") if choice == 'y' or choice == 'Y': return fun() print('Loop Exited') |
1 2 3 4 5 6 7 8 9 10 11 |
0 Do you want to exit (Y for Yes, N for No): n 1 Do you want to exit (Y for Yes, N for No): n 2 Do you want to exit (Y for Yes, N for No): n 3 Do you want to exit (Y for Yes, N for No): y Loop Exited |
Using sys
Module
Use the exit()
function of the sys
module to exit if
statement in Python.
1 2 3 4 5 6 7 8 9 |
import sys for num in range(6): print(num) if num == 3: sys.exit() print("This line will not be displayed.") print("This line will also not be displayed.") |
1 2 3 4 5 6 7 |
0 1 2 3 SystemExit |
Let’s take another example below:
1 2 3 4 5 6 7 8 9 10 |
import sys for num in range(6): print(num) choice = input("Do you want to exit (Y for Yes, N for No): ") if choice == 'y' or choice == 'Y': sys.exit() print("This line will not be displayed.") print("This line will also not be displayed.") |
1 2 3 4 5 6 7 8 9 |
0 Do you want to exit (Y for Yes, N for No): n 1 Do you want to exit (Y for Yes, N for No): r 2 Do you want to exit (Y for Yes, N for No): y SystemExit |
The code snippets resembled the examples learned using the break
statement; however, we used the sys.exit()
function here to exit if
statement in Python. The sys.exit() was used to exit the interpreter that’s why no statement was executed after the sys.exit()
function.
The
sys.exit()
not only exits theif
statement but exits the Python script as well. This is the reason, thesys.exit()
is not recommended because we have comparatively better ways to get the same results; for example,break
andreturn
statements.Remember, the
sys.exit()
function exits the script with aSystemExit
exception.
That’s all about how to exit if statement in Python.