continue Statement in Python
This tutorial is created to cover one of the most used conditional statement of Python, that is continue statement. The continue statement sometime plays an important role in creating the logical program. Therefore, I've provided all the details about it along with its examples. This article deals with:
- What is continue statement ?
- Syntax of continue statement
- Examples of continue statement
What is continue Statement ?
The continue statement/keyword is also a conditional statement like break and pass. The continue statement is used to skip the execution of remaining part (statement(s)) of the current loop and continue for the next iteration.
Syntax of continue Statement
The syntax to use continue statement is the continue keyword itself. That is, we do not need anything other attached with the statement. Here is its syntax:
Examples of continue Statement
Because you're learning the concept of computer programming in Python. Therefore without example program, how can it be possible to complete the thing. Let's start with a very simple example of continue statement:
nums = [1, 2, 3, 4, 5] for n in nums: if n == 2: continue print(n)
The output produced by this Python program is shown in the snapshot given below:
As you can see, the number 2 does not gets printed on output. Because when the condition n==2 satisfies or evaluates to be True, that is, when the value of n becomes equal to 2, then program flow goes inside the if's body and executes the continue keyword, that skips the remaining part of the loop's statement(s) to be executed, and continue for the next iteration.
Let's take another example that of course uses continue statement in a while loop:
count = 0 while count<10: count = count+1 if count>5: continue print(count)
The above program produces:
From above program, the statement:
count = count+1
executes 10 times, whereas the statement:
executes only 5 times. This is because, when the value of count becomes greater than 5, then every time the condition of if, that is count>5 evaluates to be True, and using the continue, the next iteration of the loop gets executed, and the print() statements gets skipped. Here is another program, that may clear your doubt (if any) about above program:
count = 0 while count<10: count = count+1 print("\nAbove \"continue\" Keyword") if count>5: continue print("Below \"continue\" Keyword", count)
The snapshot given below shows the sample output produced by above Python program:
As you can see, the second print() statement inside the loop, does not gets executed when the value of count is 6, 7, 8, 9, or 10.
Let's take a program that provides the master use of continue keyword in a program. This is just a hint that how you can take help of continue keyword/statement to create this type of amazing logic in your Python program/project. This is the same program with different logic, as the program created in break keyword tutorial.
num = 2 while num>1: mul = 1 print("\n---Table of", num, "---") while mul<=10: print(num, "x", mul, "=", num*mul) mul += 1 print("\nWant to continue printing Table ? (y/n) ", end="") confirm = input() if confirm == 'y': num = num+1 continue else: num = 0
The initial output produced by above program is shown in the snapshot given below:
Now to continue printing the table, enter y, otherwise enter anything else like n. Here is another sample run with user input y to print another table like shown in the snapshot given below:
And here is one more sample run, but this time with user input n:
Now you may think, what is new in above program ? The main thing to point out from above program is, we've continue printing the table using continue statement or keyword, but stopped printing table without using break keyword.
That is, when user enters y as input, then the condition confirm == 'y' evaluates to be True and program flow goes inside the if's body and the value of num gets incremented by 1, then using continue, the next iteration of the loop continues.
And when user enters n (anything other than y) as input, then the condition confirm == 'y' evaluates to be False, and the program flow goes inside the else's body and 0 gets initialized to num. Since all the statements of the loop gets executed, therefore program flow evaluates the condition num>1 again. But this time the condition num>1 or 0>1 evaluates to be False. Therefore the execution of the loop gets stopped or terminated.
« Previous Tutorial Next Tutorial »
Like/Share Us on Facebook 😋