Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Python Break

The document explains the Python break statement, which is used to exit loops prematurely. It provides examples of using break in for loops, while loops, and nested loops, demonstrating how it terminates the current loop's execution based on specified conditions. The document also includes syntax and output for each example to illustrate the functionality of the break statement.

Uploaded by

kondasanjeevarao
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Break

The document explains the Python break statement, which is used to exit loops prematurely. It provides examples of using break in for loops, while loops, and nested loops, demonstrating how it terminates the current loop's execution based on specified conditions. The document also includes syntax and output for each example to illustrate the functionality of the break statement.

Uploaded by

kondasanjeevarao
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13


Python Java JavaScript SQL C++ HTML CSS React
← prev next →

Python break statement


The break is a keyword in python which is used to bring the program control out of
the loop. The break statement breaks the loops one by one, i.e., in the case of nested
loops, it breaks the inner loop first and then proceeds to outer loops. In other words,
we can say that break is used to abort the current execution of the program and the
control goes to the next line after the loop.

The break is commonly used in the cases where we need to break the loop for a given
condition. The syntax of the break statement in Python is given below.

Syntax:

#loop statements
break;

Example 1 : break statement with for loop


Code

# break statement example


my_list = [1, 2, 3, 4]
count = 1
for item in my_list:
if item == 4:
print("Item matched")
count += 1
break
print("Found at location", count)

Output:
Item matched
Found at location 2

In the above example, a list is iterated using a for loop. When the item is matched with
value 4, the break statement is executed, and the loop terminates. Then the count is
printed by locating the item.

Example 2 : Breaking out of a loop early


Code

# break statement example


my_str = "python"
for char in my_str:
if char == 'o':
break
print(char)

Output:

p
y
t
h

When the character is found in the list of characters, break starts executing, and
iterating stops immediately. Then the next line of the print statement is printed.

Example 3: break statement with while loop


Code

# break statement example


i = 0;
while 1:
print(i," ",end=""),
i=i+1;
if i == 10:
break;
print("came out of while loop");

Output:

0 1 2 3 4 5 6 7 8 9 came out of while loop

It is the same as the above programs. The while loop is initialised to True, which is an
infinite loop. When the value is 10 and the condition becomes true, the break
statement will be executed and jump to the later print statement by terminating the
while loop.

Example 4 : break statement with nested loops


Code

# break statement example


n=2
while True:
i=1
while i <= 10:
print("%d X %d = %d\n" % (n, i, n * i))
i += 1
choice = int(input("Do you want to continue printing the table? Press 0 for no: "))
if choice == 0:
print("Exiting the program...")
break
n += 1
print("Program finished successfully.")

Output:
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20
Do you want to continue printing the table? Press 0 for no: 1
3 X 1 = 3
3 X 2 = 6
3 X 3 = 9
3 X 4 = 12
3 X 5 = 15
3 X 6 = 18
3 X 7 = 21
3 X 8 = 24
3 X 9 = 27
3 X 10 = 30
Do you want to continue printing the table? Press 0 for no: 0
Exiting the program...
Program finished successfully.

There are two nested loops in the above program. Inner loop and outer loop The inner
loop is responsible for printing the multiplication table, whereas the outer loop is
responsible for incrementing the n value. When the inner loop completes execution,
the user will have to continue printing. When 0 is entered, the break statement finally
executes, and the nested loop is terminated.

Next Topic Python Pass

← prev next →
Related Posts

Python Loops
The following loops are available in Python to fulfil the looping needs. Python
offers 3 choices for running the loops. The basic functionality of all the
techniques is the same, although the syntax and the amount of time required
for checking the condition differ. We can run...

 6 min read

Python For Loop


Python for loop Python is a strong, universally applicable prearranging language
planned to be easy to comprehend and carry out. It is allowed to get to because
it is open-source. In this tutorial, we will learn how to use Python for loops, one of
the most fundamental...

 4 min read

Python If else
Python If-else statements Decision making is the most important aspect of
almost all the programming languages. As the name implies, decision making
allows us to run a particular block of code for a particular decision. Here, the
decisions are made on the validity of the particular...

 6 min read

Python Continue
Python continue Statement Python continue keyword is used to skip the
remaining statements of the current loop and go to the iteration. In Python,
loops repeat processes on their own in an efficient way. However, there might be
occasions when we wish to leave the...

 3 min read

Python While Loop


s In coding, loops are designed to execute a specified code block repeatedly.
We&#39;ll learn how to construct a while loop in Python, the syntax of a while
loop, loop controls like break and continue, and other exercises in this tutorial.
Introduction of In this article, we...

 9 min read

Python Pass
Statement In this tutorial, we will learn more about past statements. It is
interpreted as a placeholder for future functions, classes, loops, and other
operations. What is Python&#39;s Pass Statement? The pass statement is also
known as the null statement. The Python mediator doesn&#39;t overlook a
Remark,...

 2 min read

Subscribe to Tpoint Tech


We request you to subscribe our newsletter for upcoming
updates.

Your Email Subscribe 


Learn Important Tutorial

Python Java
Javascript HTML

Database PHP

C++ React

B.Tech / MCA

Data
DBMS
Structures

Operating
DAA
System
Computer Compiler
Network Design

Computer Discrete
Organization Mathematics

Ethical Computer
Hacking Graphics

Web Software
Technology Engineering

Cyber
Automata
Security
C C++
Programming

Java .Net

Python Programs

Control Data
System Warehouse

Preparation

Aptitude Reasoning
Verbal Interview
Ability Questions

Company
Questions

You might also like