- read

Python: break, continue, and pass Statements with Examples

Rohit Kumar Thakur 74

break, continue, and pass statements in python

Hello Python enthusiasts..!!

In the last article, we talked about Python while loop and practiced some examples too. In the article, we are going to talk about Python break, continue, and pass statements.

When we write code, sometimes we need to alter the normal flow of the loop in response to the occurrence of an event. In such a situation, we may either want to exit the loop or continue with the next iteration of the loop, skipping the remaining statements in the loop.

With the help of the break statement, we can exit the loop and transfer the control to the statement following the body of the loop. The continue statement helps us to transfer the control to the next iteration of the loop. When the continue statement is executed, the code that occurs in the body of the loop after the continue statement is skipped.

Let’s understand these statements with an example. It would be more clear to you then. In this example, we are writing a Python program to print out the squares of the integer. The input must be taken from the user. If the user enters a null string then break the loop and move to the end of the statement.

while True:
num = input("Enter the number: ")
if num == "":
number = int(num)
print("The square of the number ",number," is: ", number*number)
print("User entered a null string")

The output of the code is:

>>> Enter the number: 5
The square of the number 5 is: 25
Enter the number: 6
The square of the number 6 is: 36
Enter the number: 7
The square of the number 7 is: 49
Enter the number:
User entered a null string

You can now clearly see that how the break statement work. I started the while loop and the condition of the while loop is set to True. It means that we are in an infinite loop. In this loop, the input function will ask you for the value of the integer an infinite number of times until it breaks by null string or manually. We set our condition in the if statement condition, that if the user enters a null string value then break the while loop and move the print statement (last line of our code). With the help of the break statement, here we exited the loop and transfer the control to the next statement following the body of the loop.

Let’s take another example. In this example, you have to write a Python function to print out the percentage marks of old students. Let’s write the code, it would be more clear then.

def main():
check = input("Are you a new student(yes/no): ")
if check == "yes":
elif check == "no":
totalMarks = 0
numOfSub = 0
while True:
marks = input( 'Marks for subject ' + str(numOfSub +1) + ": ")
if marks == '':
marks = float(marks)
if marks < 0 or marks >100:
print("Invalid marks")
numOfSub = numOfSub+1
totalMarks = totalMarks + marks
percentage = totalMarks/numOfSub
print("Total marks: ", totalMarks)
print("Number of Subject is: ", numOfSub)
print("Percentage: ", round(percentage,2))
print("enter a valid response")

The output of the above code is:

>>> Are you a new student(yes/no): no
Marks for subject 1: 56
Marks for subject 2: 68
Marks for subject 3: 98
Marks for subject 4: 88
Marks for subject 5: 94
Marks for subject 6:
Total marks: 404.0
Number of Subject is: 5
Percentage: 80.8
>>> Are you a new student(yes/no): yes
>>> Are you a new student(yes/no): sdgfs
enter a valid response
>>> Are you a new student(yes/no): no
Marks for subject 1: 45
Marks for subject 2: 566
Invalid marks
Marks for subject 2: 56
Marks for subject 3: 65
Marks for subject 4:
Total marks: 166.0
Number of Subject is: 3
Percentage: 55.33

In the main function, we first check out for new students or old students. If the student is old then we proceed with asking subject-wise marks. We take the input of marks and make sure that the marks lie between 0 and 100. If the user enters marks greater than 100 or less than 0 then the program prints out “Invalid marks” and continues for marks of other subjects. Here we used the continue statement. If the user inputs a null string then, the loop break, we come out of the while loop and print out the percentage. Here we used the break statement. If the student is new then we simply pass this condition. Because sometimes we may want to leave out the details of the computation in a function body or in some conditional statements, to be filled in at a later point of time. The pass statement lets the program go through this piece of code without executing any code. Here, in this case, we will fill out the code for new students at some point in time but not now.

Well, That’s it for this article.

If this article sounds informative to you then make sure to follow and share it with your geek community.