- 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 CGI Programming
- Python Network Programming
- Python Send E-mail
- Python Multi-threading
- Python XML Processing
- Python MySQL Database
- Python GUI Programming
- 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
Difference between break and continue in Python
This tutorial is created to clear your doubt about one of the famous comparison between two conditional statements of Python programming language, that is break and continue statement.
break Vs. continue
The table given below differentiates break and continue keyword:
break | continue |
---|---|
leaves the loop | jumps for next iteration |
skips remaining execution of complete loop | skips execution of remaining statement(s) inside the loop for current iteration |
useful, if condition always evaluates to be True | useful, if wants to skip execution of some statement(s) inside the loop for any particular iteration |
break Vs. continue Example
I's sure, after understanding the example program given below, you will get the complete understanding about break Vs. continue keyword in Python. That is, how these two keywords or statements are used in Python programming:
nums = [1, 2, 3, 4, 5] print("----Using \"break\"-------") for n in nums: if n == 3: break print(n) print("----Using \"continue\"-------") for n in nums: if n == 3: continue print(n)
This program produces the output as shown in the snapshot given below:
As you can see from above program, the first block of code, that is:
for n in nums: if n == 3: break print(n)
prints only 1, 2. Because, when the value of n becomes 3, then the condition n == 3 or 3 == 3 evaluates to be True, and using break, the execution of loop gets terminated. Whereas the second part, that is the following block of code:
for n in nums: if n == 3: continue print(n)
prints all the numbers except 3. Because, when the condition n == 3 evaluates to be True, therefore using continue, the program flow jumps to the next iteration of the loop, and the remaining statement(s) after the keyword continue, that is print(n) gets skipped.
« Previous Tutorial Next Tutorial »