Python break, continue and pass Statements with Examples

In this article will talk about Python break, continue and pass Statements. If you didn’t see our last article Understanding While Loop in Python with Examples then click the link and know more about python.

When writing code, we sometimes need to change the loop’s typical flow in reaction to a specific event. In this case, we may either leave the loop or go to the next iteration of the loop, bypassing the remaining statements in the loop.

We may escape the loop and pass control to the statement after the loop’s body with the help of the break statement. The continue statement allows us to pass control to the loop’s next iteration. The code in the body of the loop after the continue statement is bypass when the continue statement is perform.

break, continue and pass Statements

Let’s look at an example to better grasp these claims. Then it would be clearer to you. In this example, we’ll write a Python program that outputs the integer’s squares. The user’s input must be obtain. Break the loop and move to the conclusion of the statement if the user inputs a null string.

Example of break 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")


>>> 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 see how the break statement functions now. The while loop was begun, and the while loop’s condition was set to True. It suggests we’re stuck in an endless circle. The input function will ask you for the integer value an endless number of times in this loop until it breaks by null string or explicitly. We put our condition in the if statement to interrupt the while loop and shift the print statement if the user provides a null string value (last line of our code). We left the loop and transferred control to the next statement after the loop’s body with the help of the break statement.

Example of break, continue and pass statements

Let’s look at another scenario. You need to develop a Python function to output the percentage marks of old students in this example. Let’s write the code; it’ll be easier to understand.

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")


>>> 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
  • We initially check out for new or old students in the primary function. If the kid is older, we ask for subject-by-subject grades. We take the marks’ input and ensure that they are between 0 and 100. If the user inputs a mark that is larger than 100 or less than 0, the computer outputs “Invalid marks” and moves on to the next subject’s marks. We utilized the continue statement in this case.
  • If the user enters a null string, the loop is broken, and we exit the while loop to print the percentage. We utilized the break statement in this case.
  • We just pass this condition if the student is new. Because we may wish to leave out the specifics of the computation in the body of a function or in certain conditional expressions so that they may be filled in later. The pass statement instructs the program to skip over this section of code without executing it.

If you want to learn more about python then visit the official documentation of python.

That’s all for this article if you have any confusion contact us through our website or email us at [email protected]

Leave a Comment