Continue and Break Statement in Python

continue-and-break-statement
  • Save

Are you tired of finding the best explanation for the continue and break statements in Python? In your mind, there are many questions that will be there, what is the continue and break statement in Python? Why does a programmer use continue and break statements in the programs? What is the difference between continue and break statement and many other questions.Let’s start with these.

Table of Contents

Why do we need a Control and break statement?

Let’s see why we need a continue and break statement in our program. Till now, we have studied loops in Python, and if we want to terminate or exit from the loop, we have to wait until it does not satisfy the condition or an expression.

For example, if I have made a loop of the 1000th iteration and I want to stop it at the 100th iteration. So it will not stop or exit from the loop until it reaches the 1000th iteration. So, here it takes more memory space and consumes more time.

Here, comes the role of the continue and break statements in Python.

Continue Statement in Python

  1. The continue statement, as its name suggests, completes the loop to continue or carry out the subsequent iteration.
  2. When the continue statement in a loop is executed, the code that follows the continue statement is skipped, and the loop then moves on to the next iteration.

Syntax:

continue

Continue Statement in Python
  • Save
Continue Statement in Python

Example of Continue Statement in Python:

""" continue statement"""
"""print odd number"""

for i in range(5, 20):
    if i % 2 == 0:
        continue       ## skip to the next iteration
        print(i, end=' ') 
    else:
        print(i, end=' ')
        
#Output :
5 7 9 11 13 15 17 19 

Here, the loop starts from the 5 and runs till the 19. So, it will start from 5 and checks the if condition. If the “if” condition is satisfied then the control flows inside the “if statement”.

Here, in the first iteration, it does not satisfy the if the condition so it will go to the else part and print 5.

So, again, it will go for the next iteration, which is 6, and check the if condition. If it satisfies the condition, it will read the continue statement and directly go to the next iteration, which is 7. It will not print 6, because when it executes the continue statement, it will directly move to the next iteration. It will never execute a print statement because as soon as it reads the continue statement, it will leave all the statements under that and move to the next iteration.

Now, let us make a small project.

Q) There is a company in a town named “Bin-Fin Food product”. So, the government of that country announced that we randomly put 7 food in a basket and if 3 foods are found to be rotten then the Company will shut down and the owner of the company will have to pay a severe fine.

""" continue statement"""
basket1 = ["Clean", "Rotten", "Clean", "Rotten", "Clean", "Clean", "Rotten"]
rotten = 0
clean = 0

for i in basket1:
    if i == "Rotten":
        rotten += 1
        continue
    else:
        clean += 1
if rotten >= 3:
    print("Company had been shut down")
else:
    print("Compnay can continue to store foods")

    
#Output:
Company had been shut down

Break statement in Python

  1. The break statement is also a loop control statement just like a continue statement. A break statement is exactly the opposite of a continue statement.
  2. It is used to terminate the loop or statement inside which is present.
  3. The statements that are present after the break statement, if any, will thereafter take control if they are present. If a break statement is present in a nested loop, only those loops that contain a break statement are terminated.

Syntax:

break

Break Statement in Python
  • Save
Break Statement in Python

Example of Break Statement

"""Break statement"""
for i in range(1, 10):
    if i == 6:
        break
    else:
        print(i, end=' ')
        
#Output :
1 2 3 4 5 

Here, the loop starts at 1 and ends at 9.

In the first iteration, it will check if the condition is satisfied or not. If not, then it will move to the else block and execute the print statement.

It will run until the 5th iteration, and on the 6th iteration, it will satisfy the “if condition” and execute the break statement.

As soon as it executes the break statement, it will terminate the loop.

Now, let’s make a small project.

Q) There is a shop in a town named “Bin-Fin lottery.” The owner of the company has decided to give a reward to the person who won the lottery.

"""Break statement"""
dict1 = {"Sam": 35, "Tom": 90, "Jerry": 87, "Oscar": 56, "Patrick": 87}
lottery = 87

for person, num in dict1.items():
    If num equals lottery:
        print(person, 'has won the lottery with the number', num)
        break
    else:
        print(person, 'has lost the lottery')
        
#Output:
Sam has lost the lottery
Tom has lost the lottery
Jerry has won the lottery with the number 87

The program contains a dictionary of customers, with a variable dict1 having the data type of the dictionary. Here, a lottery variable stores the winner’s number.

dict1 stores the result of the customer’s name and number.

We used the dictionary’s items() function to separate the key value and store the result in person, as well as the num variable to iterate in the for a loop. The person and num variables store the result of the customer’s name and lottery number, respectively.

Difference between continue and break statements in python

statements in python

Continue StatementBreak Statement
It terminates only the current iteration of the loop.It terminates the execution of the remaining iteration of the loop.
“Continue” resumes the flow of the program for the next iteration of that enclosing “continue.”“break” returns control to the program at the end of the loop that contains “break.”.
“Continue’ does not stop the continuation of the loop, it only stops the current iteration.‘break’ stops the continuation of the loop.
It causes the early execution of the next iteration.It early terminates the loop.
It cannot be used with switch statements and labels.Can be used with switch statements and labels.
Difference between continue and break statement

Pass statement in Python

  1. The pass statement simply does nothing as the name suggests.
  2. When a statement is syntactically necessary but no command or code is to be executed, the pass statement in Python is used.
  3. It is similar to a null operation in that it has no effect.
  4. The pass statement can be used to create empty loops as well. Pass is also used for classes, functions, and empty control statements.

Syntax:

pass

## Demo of Pass Statement 

company_name = "BinFin Tech"

for i in company_name:
    if i == 'T':
        print("Pass Executed")
        pass
      


print("Loop Completed")


## Output:
B
i
n
F
i
n
 
Pass Executed
T
e
c
h
Loop Completed

We can conclude from the above example when the value of ‘i’ becomes equal to ‘T’, the pass statement did nothing and hence the letter ‘k’ is also printed.

Difference between Continue and Pass Statement in Python.

Statement in Python.

Continue StatementPass Statement
When the program executes the continue statement, it rejects the remaining code in the current iteration and the program flow again goes to the same loop and starts the next iteration.It is used when a statement is required syntactically.
Returns the control to the beginning of the loop.Nothing happens when we execute the pass statement.
It can be used in for and while loops.It is a Null Operation.
Syntax: continueSyntax: pass
It is mainly used inside a condition in a loop.During the byte-compile phase, the pass statement is discarded.
Continue and Pass Statement in Python

What is the difference between a continue and a break statement?

The continue statement terminates only the current iteration of the loop.
The break statement terminates the execution of the remaining iteration of the loop.

“Continue” resumes control of the program for the next iteration of that loop enclosing “continue.”
“break” resumes control of the program at the end of the loop enclosing that “break.”

What is a continue and break statement in Python?

A control statement in Python is used to skip the rest of the code inside the loop for the current iteration only.
A break statement in Python is used to terminate the loop or statement immediately when it is encountered.

Why do we need to continue and break statements in Python?

Python’s use of loops makes it efficient to automate and repeat tasks. However, there may occasionally be a situation where you wish to skip an iteration, ignore the condition, or totally quit the loop. That’s where we need a continue and break statement.

Can we use continue and break statements without a loop?

No, you can’t. You can only use the return keyword; to discontinue code execution in an
if statement.

What is the pass statement?

The pass statement simply does nothing, as its name suggests:
When a statement is syntactically necessary but no command or code is to be executed, the pass statement in Python is used.
It is similar to a null operation in that it has no effect.

What is a continue statement in Python?

The continue statement, as its name suggests, completes the loop to continue or carry out the subsequent iteration.
When the continue statement in a loop is executed, the code that follows the continue statement is skipped, and the loop then moves on to the next iteration.

What is a break statement in Python?

1. The break statement is also a loop control statement, just like the continue statement.
2. A break statement is exactly the opposite of a continue statement.
3. A break statement is used to terminate the loop or statement inside which it is present.

Prerequisite for Learning Python

Download the Python module from the given link. Click Here

Write blogs related to Ethical hacking, Computer networks, Linux, Penetration testing and Web3 Security.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top
0 Shares
Share via
Copy link