Course Content
Python Indentation, Comments and Variables
0/2
Object Oriented Programming in Python
0/1
Exception Handling in Python
0/1
Sending emails with Python
0/1
Unit test in python programming
0/1
Python programming (zero to advance!!!)
About Lesson

In Python, `break`, `continue`, and `exit` are control flow statements used within loops to alter the flow of execution.

1. `break` statement:

The `break` statement in Python is used to exit a loop prematurely. When the `break` statement is encountered within a loop, the loop is terminated immediately, and the program continues with the next statement after the loop.

Here are a few illustrative examples demonstrating the use of the `break` statement:

### Example 1: Exiting a Loop When a Condition is Met

“`python
# Exit the loop when the number becomes greater than 5
for num in range(10):
     print(num)
     if num > 5:
          break
“`

Output:
“`
0
1
2
3
4
5
6
“`

### Example 2: Terminating an Infinite Loop

“`python
# Exit the loop when the user enters ‘quit’
while True:
       command = input(“Enter a command (type ‘quit’ to exit): “)
       if command == ‘quit’:
                  break
      print(f”Executing command: {command}”)
“`

### Example 3: Exiting Nested Loops

“`python
# Exit both loops when ‘break’ is encountered
for i in range(3):
      print(f”Outer loop iteration: {i}”)
      for j in range(3):
             print(f”Inner loop iteration: {j}”)
             if j == 1:
                     break # Exit the inner loop
“`

Output:
“`
Outer loop iteration: 0
Inner loop iteration: 0
Inner loop iteration: 1
Outer loop iteration: 1
Inner loop iteration: 0
Inner loop iteration: 1
Outer loop iteration: 2
Inner loop iteration: 0
Inner loop iteration: 1
“`

In each of these examples, the `break` statement allows us to exit the loop prematurely based on certain conditions or events. It provides a way to control the flow of the program and terminate the loop when necessary.

2. `continue` statement:

The `continue` statement in Python is used to skip the rest of the code inside a loop for the current iteration and proceed to the next iteration of the loop. When the `continue` statement is encountered within a loop, the current iteration is terminated immediately, and the loop proceeds with the next iteration.

Here are a few illustrative examples demonstrating the use of the `continue` statement:

### Example 1: Skipping Odd Numbers

“`python
# Print only even numbers from 1 to 10
for num in range(1, 11):
        if num % 2 != 0:
             continue
       print(num)
“`

Output:
“`
2
4
6
8
10
“`

### Example 2: Skipping Specific Values

“`python
# Print numbers from 1 to 5, skipping 3
for num in range(1, 6):
        if num == 3:
             continue
        print(num)
“`

Output:
“`
1
2
4
5
“`

### Example 3: Skipping Items in a List

“`python
# Print items from a list, skipping ‘skip_me’
items = [‘apple’, ‘banana’, ‘cherry’, ‘date’]
for item in items:
       if item == ‘banana’:
            continue
       print(item)
“`

Output:
“`
apple
cherry
date
“`

In each of these examples, the `continue` statement allows us to skip certain iterations of the loop based on specific conditions. It provides a way to control the flow of the loop and proceed to the next iteration without executing the remaining code inside the loop for the current iteration.

 

3. `exit()` function:

The `exit()` function is used to terminate the execution of a Python script or program. When the `exit()` function is called, it immediately terminates the program, and no further code after the `exit()` function call is executed.

Here’s a basic example illustrating the use of the `exit()` function:

“`python
import sys

print(“Before exit() function”)

# Call exit() function to terminate the program
sys.exit()

print(“After exit() function”) # This line will not be executed
“`

In this example:

– The `import sys` statement imports the `sys` module, which provides access to some variables used or maintained by the Python interpreter and to functions that interact with the interpreter.
– The `print(“Before exit() function”)` statement prints a message indicating that it is executed before calling the `exit()` function.
– The `sys.exit()` function call immediately terminates the program.
– The `print(“After exit() function”)` statement is never executed because the program has already been terminated by the `exit()` function.

It’s important to note that the `exit()` function is part of the `sys` module, so you need to import the `sys` module before using the `exit()` function in your code. Additionally, calling `exit()` without any arguments terminates the program without raising an exception.

 

In summary, `break` is used to exit a loop prematurely, `continue` is used to skip the current iteration and move to the next one, and `exit()` is used to terminate the entire Python program immediately. Each of these statements and functions serves a distinct purpose in controlling the flow of your Python code.

Join the conversation