- Python Basics
- Python Tutorial
- Python Applications
- 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 and 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 and Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes and Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class vs. Static Method
- Python @property Decorator
- Python Keywords
- Python 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
Difference between break and continue in Python
This tutorial is created to clear your doubts about one of the famous comparisons between two conditional statements in the Python programming language, namely the break and continue statements.
break vs. continue in Python
The table given below differentiates break and continue keywords:
break | continue |
---|---|
leaves the loop | jumps for the next iteration |
skips the remaining execution of the loop. | skips execution of the remaining statement(s) inside the loop for the current iteration. |
If the condition always evaluates to true, it is useful. | useful if one wants to skip execution of some statement(s) inside the loop for any particular iteration. |
break vs. continue example in Python
I'm sure that after understanding the example program given below, you will have a complete understanding of the break vs. continue keywords in Python. In Python programming, this is how these two keywords or statements are used:
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 the above program, the first block of code is:
for n in nums: if n == 3: break print(n)
prints "1, 2." Because when the value of n becomes 3, the condition "n == 3" or "3 == 3" evaluates to be true, and using break, the execution of the loop gets terminated. Whereas the second part 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), get skipped for that iteration.
« Previous Tutorial Next Tutorial »