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.
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
- 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.
Syntax:
continue
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
- 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.
- It is used to terminate the loop or statement inside which is present.
- 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
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 Statement | Break 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. |
Pass statement in Python
- The pass statement simply does nothing as the 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.
- 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 Statement | Pass 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: continue | Syntax: pass |
It is mainly used inside a condition in a loop. | During the byte-compile phase, the pass statement is discarded. |
FAQ Related Questions to Continue and 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.”
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.
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.
No, you can’t. You can only use the return keyword; to discontinue code execution in an
if 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.
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.
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