In Python, break and continue statements have the capacity for altering the flow of a normal loop. Moreover, loops iterate over a block of code until the test expression is false. However, sometimes you may wish to terminate the current iteration or even the whole loop without having to check the test expression. Thus, these cases make use of break and continue statements.
When we use loops in Python, it automates and repeats the tasks in an effective manner. However, sometimes, there may arise situations where one wants to exit the loop completely, skip and iteration or avoid that condition. One can do this by loop control statements. In other words, loop control statements change execution from its normal sequence.
When execution will leave a scope, all the automatic objects that were produced in that scope get destroyed. Python supports certain control statements which are:
- Continue statement
- Break statement
- Pass statement
Through this article, we will throw light on the continue statement.
It refers to a loop control statement just like the break statement. Moreover, this kind of statement is the opposite of what the break statement is. In other words, instead of terminating the loop, it will force to execute the next iteration of the loop.
As the term itself suggests, this kind of statement forces the loop to continue or execute the next iteration. Thus, when this statement executes in the loop, the code inside the loop following the continue statement gets skipped and the next iteration of the loop begins.
Flowchart of Continue
In this chart, we will go through the steps given here.
Step 1) The loop execution begins.
Step 2) The execution of code inside the loop will be completed. If there is a continued statement inside the loop, the control goes back to Step 4. Meaning to say, the start of the loop for the next iteration.
Step 3) The execution of code inside the loop takes place.
Step 4) If there is a continue statement or the loop execution inside the body happens, it will call the next iteration.
Step 5) Once the loop execution completes, the loop will exit and proceed to step 7.
Step 6) If the loop condition in step 1 will fail, it will exit the loop and proceed to step 7.
Step 7) It is the end of the loop.
Browse more Topics Under Conditional Constructs and Looping
- If Else Statement While
- For (Range Function)
- Nested if
- Nested Loops
- Use of Compound Expression in Conditional and Looping Construct
Let us assume that you have a list of numbers and you wish to print only the odd numbers out of that list. You can carry this out by making use of the continue statement. The print statement inside the loop will be skipped by making use of the continue statement when the number is even. Through this, all the even numbers get skipped and the print statement executes for all the odd numbers.
# program to display only odd numbers for num in [20, 11, 9, 66, 4, 89, 44]: # Skipping the iteration when number is even if num%2 == 0: continue # This statement will be skipped for all even numbers print(num)
11 9 89
Let us take a situation where we need to write a program that will print the number from 1 to 10 and but not 6. It specifies that one has to do this by making use of a loop and only one loop is allowed to use.
Thus, now the need for using this kind of statement will arise. What you can do over here is run a loop from 1 to 10, every time you need to compare the value of the iterator with 6. If it will be equal to 6, you will use this statement to continue to the next iteration without having to print anything otherwise you will print the value.
Take a look below for the implementation of the above idea:
# Python program to # demonstrate continue # statement # loop from 1 to 10 for i in range(1, 11): # If i is equals to 6, # continue to next iteration # without printing if i == 6: continue else: # otherwise print the value # of i print(i, end = " ")
1 2 3 4 5 7 8 9 10
This kind of statement can come in use with any other loop also like while in a similar way as it comes in use with for loop above.
# Program to show the use of this statement inside loops for val in "string": if val == "i": continue print(val) print("The end")
s t r n g The end
This program is similar to the above example except the break statement gets replaced with continue. We will continue with the loop, if the string is i, not executing the rest of the block. Thus, we see in the output that all the letters except i will be printed.
FAQ on Continue
Question 1: What is the use of continue in Python?
Answer 1: This kind of statement in Python returns the control to the beginning of the while loop. Moreover, it will reject all the remaining statements in the current iteration of the loop and will move the control back to the top of the loop. Thus, this kind of statement can come into use both while and for loops.
Question 2: How do you continue in Python 3?
Answer 2: Ths kind of statement in Python returns the control to the beginning of the current loop. When encountered, the loop begins the next iteration without executing the remaining statements in the current iteration. Thus, this kind of statement can come into use in both while and for loops.
Question 3: What is the difference between a pass and continue in Python?
Answer 3: Pass and continue are completely different things in Python. Pass simply does not do anything while continue goes on with the next loop iteration. In other words, continue will force the loop to begin at the next iteration whereas pass means that there is no code to execute here. Further, it will jump back to the top of the loop.
Question 4: Why do we need iterator in python?
Answer 4: We need iterators in Python as they allow lazy evaluation. Thus, they only produce the next element of an iterable object when requested. This is beneficial for very large data sets. Moreover, iterators and generators can only be iterated over once. Most importantly, Generator Functions are said to be better than Iterators.