Break, Continue, and Pass Statements with Examples
Python Tutorial Series: Learning to use the break, continue, and pass statements.

Hello Python Enthusiasts!
In the last article, we talked about Python while loop and practised some examples too. In the article, we are going to talk about Python break, continue, and pass statements.
Attention all developers seeking to make social connections and establish themselves while earning passive income — look no further! I highly recommend ‘From Code to Connections’, a book that will guide you through the process. Don’t miss out, grab your copy now on Amazon worldwide or Amazon India! You can also go for Gumroad
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 == "":
break
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 exit 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":
pass
elif check == "no":
totalMarks = 0
numOfSub = 0
while True:
marks = input( 'Marks for subject ' + str(numOfSub +1) + ": ")
if marks == '':
break
marks = float(marks)
if marks < 0 or marks >100:
print("Invalid marks")
continue
numOfSub = numOfSub+1
totalMarks = totalMarks + marks
percentage = totalMarks/numOfSub
print("Total marks: ", totalMarks)
print("Number of Subject is: ", numOfSub)
print("Percentage: ", round(percentage,2))
else:
print("enter a valid response")
main()
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 use the continue statement.
If the user inputs a null string then, the loop breaks, we come out of the while loop and print out the percentage. Here we use the break statement.
If the student is new then we simply pass this condition. Because sometimes we may want to leave out the computation details in a function body or in some conditional statements, to be filled in at a later point. 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.
You can practice more questions:
Least Common Multiple (LCM) in Python
Python Function to Check If a Number is Prime or Not
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.
Hello, My Name is Rohit Kumar Thakur. I am open to freelancing. I build React Native projects and I am currently working on Python Django. Feel free to contact me at (freelance.rohit7@gmail.com).
More content at plainenglish.io