Break and Continue Statement
Python Basics
Python Introduction
Python Installation
Overview of Jupyter IDE
Identifiers & Reserved Keywords
Python Variables
Python Numbers
Python Operators
Python Operators and Arithmetic Operators
Comparison and Logical Operators
Assignment and Bitwise Operators
Identity and Membership Operators
Python Flow Control
if else if else statement
While Loop Statement
Python For Loop
Break and Continue Statement
Python Data Types
Python Strings
Python Strings Methods
Python Lists
Python Tuples
Python Dictionary
Python Functions
Introduction to Python Functions
Function Arguments
Recursion Function
Lambda/Anonymous Function
Python - Modules
Python Files
Python - Files I/O
Python - Exceptions Handling
Python - Debugging
Python Break Statement
Syntax
break
Example
# Program to print the fruits available inside the list fruits = ["Apple", "Banana", "Cherry","Mango","Pineapple","Jackfruit"] for x in fruits: print(x) if x == "Mango": break Output: Apple Banana Cherry Mango
# Program to print numbers in ascending order till the mentioned limit i = 1 while i <= 10: print(i) i += 1 if i == 5: break print("Execution Completed!") Output: 1 2 3 4 Execution Completed!
Note:If you are using nested loops, the break statement stops the execution of the innermost loop and start executing the next line of code after the block.
# Nested for loop example without break statement for x in range(1,4): # 1 ,2, 3 ### Inner for loop ### for y in range(1,4): print(x,y) Output: 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3
# Nested for loop with break statement for x in range(1,4): # 1 ,2, 3 ### Inner for loop ### for y in range(1,4): print(x,y) if x == 2 and y == 2: break Output: 1 1 1 2 1 3 2 1 2 2 3 1 3 2 3 3
Python Continue Statement
Syntax:
continue
Example
# Program to print numbers in ascending order till the mentioned limit i = 0 while i <= 10: i += 1 if i == 5: continue print(i) print("Execution Completed!") Output: 1 2 3 4 6 7 8 9 10 11 Execution Completed!
#print even numbers present in a list numbers = [1, 2, 3, 4, 5, 9, 20 , 23, 36] for num in numbers: if num % 2 != 0: continue print(num) Output: 2 4 20 36