Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Python Break

by suresh

The Python Break and Continue Statements are two important statements used to alter the flow of a program in any programming language. We would like to share two examples to display the working functionality of the Python Break statement in both For loop and While loop

Loops are used to execute certain block of statements for n number of times until the test condition is false. There will be some situations where, we have terminate the loop without executing all the statements. In these situations we can use Python Break statement and Continue statements.

Python Break Statement

The Python Break statement is very useful to exit from any loop such as For Loop, While Loop and Nested Loops. While executing these loops, if the compiler finds the break statement inside them, the compiler will stop executing the statements inside the loop and exit immediately from the loop.

For example, we have 5 statements inside the loop and we want to exit from the loop when certain condition is True otherwise, it has to execute them. In these situations we can place the Break statement inside the If statement. If the condition is True then compiler will execute the break statement. It means, the python break statement will exit the controller from the loop completely otherwise, it will execute all the statements.

Python Break Syntax

The syntax of the Python Break Statement is as follows:

break

Python Break Statement in For Loop

In this Python program, We are going to use the break statement in Python For Loop to exit from the loop iteration.

# Python Break Statement in For Loop Example

for i in range(0, 11):
    if i == 6:
        break
    print("The Value of the Variable i is: ", i)

OUTPUT

Python Break in For Loop

ANALYSIS

In this Python Break statement in for loop example, First, we used the for loop with range() and we are not going to explain the for loop here. If you don’t understand for loop then please visit our article Python For Loop.

for i in range(0, 11):

Inside the For loop we placed If Statement to test whether I is equal to 6. If the condition is false then it will skip the Break statement and prints that number as output (In Our case 0, 1, 2, 3, 4, 5) using the following print statement.

print("The Value of the Variable i is: ", i)

If this condition ( i == 6) is True then Break statement will be executed and the iteration will stop at that number without printing the following print statement:

print("The Value of the Variable i is: ", i)

Python Break Statement in While Loop

In this program, We are going to use the Python break statement inside the While loop to exit from the loop iteration.

# Python Break Statement in While Loop Example

i = 0
while i <= 10:
    print(" The Value of the Variable i =  ", i)
    i = i + 1
    if i == 4:
        break

OUTPUT

Python Break in While Loop

ANALYSIS

In this Python break while loop example, We initialized the value of i as: i = 0 at the beginning of the code. Within the While loop, we check for the condition whether i is less than or equal to 10 or not. If you find difficult to understand the Python While loop then please visit our article: Python While loop

while i <= 10:

Inside the While loop we placed Python If Statement to test whether I is equal to 4. If the condition is false then it will skip the Break statement and prints that number as output (In Our case 0, 1, 2, 3) using the following print statement.

print(" The Value of the Variable i =  ", i)

If this condition (i == 4) is True then Break statement will be executed and the iteration will stop at that number without printing the following print statement.

print(" The Value of the Variable i =  ", i)

NOTE: We also used Python Arithmetic Operators + operator to increment the i value (i = i +1). If you forgot this line then you will end up in infinite loop

Please refer Python Continue Statement, For Loop, and While Loop

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python Numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python Numpy concatenate
  • Python Numpy Array shape
  • Python Pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python Substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy