Saturday, January 1, 2022

How to use Break, Continue and Pass statements in Python Program

What are break, continue and pass statements in Python Program
Break : 
The 'break' statement in Python Programming is used inside a For loop or While loop to come out or exit the loop. When 'break' statement is executed, the Python interpreter jumps out of the loop to process the next statements block in the program.

Continue : 
The 'continue' statement in Python Programming is used inside a For loop or While loop to skip the further execution of the current iteration and go back to the next iteration of the loop. When the 'continue' statement is executed, the next statements in the same loop are not executed further.

Pass : 
The 'pass' statement in Python Programming will not do any specific operation. It will be used in the Loops or If conditions to represent no operation. However we use the 'pass' statement when we need a statement syntactically but do not to perform any specific operation.

Example:
We can understand the usage of the above 3 statements from the following program.
# A Python program to print Even and Odd numbers between 0 to 15
import sys
print("Python Version: ", sys.version)

x = 0
while x <= 30:
    x = x + 1
    if (x-1) % 2 == 0:
        print('The even number is = ', x-1)
        continue  # Move back to next iteration
    else:
        pass  # Continue to next steps
    if x > 15:
        break  # Exit the loop
    print('The odd number is = ', x-1)
print('The loop is exited after {0} iterations'.format(x-1))

# ------------------------------------------------------------------------------------------------------------------------ #
Output:
C:\Users\tpreddy\PycharmProjects\pythonProject\PyLab1\venv\Scripts\python.exe C:/Users/tpreddy/PycharmProjects/pythonProject/PyLab1/Temp1.py
Python Version:  3.10.1 (tags/v3.10.1:2cd268a, Dec  6 2021, 19:10:37) [MSC v.1929 64 bit (AMD64)]
The even number is =  0
The odd number is =  1
The even number is =  2
The odd number is =  3
The even number is =  4
The odd number is =  5
The even number is =  6
The odd number is =  7
The even number is =  8
The odd number is =  9
The even number is =  10
The odd number is =  11
The even number is =  12
The odd number is =  13
The even number is =  14
The loop is exited after 15 iterations

Process finished with exit code 0

# ------------------------------------------------------------------------------------------------------------------------ #

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog