- Python Basics
- Python Home
- Python History
- Python Applications
- Python Features
- Python Versions
- Python Environment Setup
- Python Basic Syntax
- Python end (end=)
- Python sep (sep=)
- Python Comments
- Python Identifiers
- Python Variables
- Python Operators
- Python Ternary Operator
- Python Operator Precedence
- Python Control & Decision
- Python Decision Making
- Python if elif else
- Python Loops
- Python for Loop
- Python while Loop
- Python break Statement
- Python continue Statement
- Python pass Statement
- Python break Vs continue
- Python pass Vs continue
- Python Built-in Types
- Python Data Types
- Python Lists
- Python Tuples
- Python Sets
- Python frozenset
- Python Dictionary
- List Vs Tuple Vs Dict Vs Set
- Python Numbers
- Python Strings
- Python bytes
- Python bytearray
- Python memoryview
- Python Misc Topics
- Python Functions
- Python Variable Scope
- Python Enumeration
- Python import Statement
- Python Modules
- Python operator Module
- Python os Module
- Python Date & Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes & Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class Vs Static Method
- Python @property Decorator
- Python Regular Expressions
- Python Send E-mail
- Python Event Handling
- Python Keywords
- Python All Keywords
- Python and
- Python or
- Python not
- Python True
- Python False
- Python None
- Python in
- Python is
- Python as
- Python with
- Python yield
- Python return
- Python del
- Python from
- Python lambda
- Python assert
- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python read()
- Python write()
- Python open()
- Python Examples
- Python Examples
- Python Test
- Python Online Test
- Give Online Test
- All Test List
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:
continue
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:
print(count)
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 »
Follow/Like Us on Facebook
Subscribe Us on YouTube