Wednesday, March 11, 2026

Python break Statement With Examples

Have you ever found yourself stuck in a loop that just won't quit? In this tutorial, you’ll master the break statement in Python, your primary tool for stopping a for or while loop dead in its tracks. Whether you've found the data you were looking for or hit a specific error condition, the break keyword gives you ultimate control over your program's flow.

How the break Statement in Python Works

When the Python interpreter encounters a break statement, it immediately terminates the current loop. The program "jumps" out of the loop block and moves straight to the very next line of code.

Key Use Cases and Best Practices

The break statement in Python is most effective when paired with an if statement. This allows you to exit a loop only when a specific criteria is met, saving valuable processing time.

  • Conditional Exit: Use if to check a condition; if True, trigger the break.
  • Search Optimization: Stop searching a list once the target item is found.
  • Nested Loops: Note that when using a break inside nested loops, Python only terminates the innermost loop where the statement resides. The outer loops will continue running as usual.

break statement Python examples

1- Using break statement with for loop in Python. In the example a tuple is iterated using a for loop to search for a specific number as soon as that number is found you need to break out of for loop.

numbers = (89, 102, 0, 234, 67, 10, 333, 32)
flag = False
for num in numbers:
    if num == 10:
        flag = True
        break
if flag:
    print('Number found in tuple')
else:
    print('Number not found in tuple')

Output

Number found in tuple

As you can see here break statement is used inside for loop to break out of loop as soon as the condition is satisfied (Search Optimization use case). Note that searching for an element in a tuple can be done in a more compact way like given below, above example is more of an illustration of break statement.

if searched_num in numbers:
    print('Number found in tuple')
else:
    print('Number not found in tuple')

2- Using break statement in Python with while loop. In the example there is an infinite while loop that is used to prompt user for an input. Condition here is that entered number should be greater than 10 to break out of while loop otherwise prompt user again to enter valid input.

while True:
   num = int(input("Enter a number greater than 10: "))
   # condition for breaking out of loop
   if num > 10:
       break
   print("Please enter a number greater than 10...")

print("Entered number is", num)

Output

Enter a number greater than 10: 7
Please enter a number greater than 10...
Enter a number greater than 10: 11
Entered number is 11

3- In this example we’ll see how to use break statement with nested loops. There are 2 for loops in the example and break statement is used in the scope of inner for loop so it breaks out of that loop when the condition is true.

for i in range(1, 6):
    print(i, "- ", end='')
    for j in range(1, 10):
        print('*', end='')
        # print only 5 times then break
        if j >= 5:
            break
    # move to next line now
    print()

Output

1 - *****
2 - *****
3 - *****
4 - *****
5 - *****

As you can see though the range of inner loop is (1..10) but it breaks out when j’s value is 5 because of the break statement. The end='' used in print statement ensures that the cursor doesn’t move to next line after printing in each iteration.

That's all for this topic Python break Statement With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Python Tutorial Page


Related Topics

  1. Python continue Statement With Examples
  2. pass Statement in Python
  3. Python assert Statement
  4. Python First Program - Hello World
  5. Python String split() Method

You may also like-

  1. Name Mangling in Python
  2. Inheritance in Python
  3. Class And Object in Python
  4. Python Generator, Generator Expression, Yield Statement
  5. Java Variable Types With Examples
  6. Ternary Operator in Java With Examples
  7. How to Convert Date And Time Between Different Time-Zones in Java
  8. Spring Constructor Based Dependency Injection

Tuesday, March 10, 2026

ArrayList in Java With Examples

The ArrayList in Java is one of the most popular and versatile collection classes, widely used because of its ability to grow dynamically. Unlike traditional arrays, you don’t need to anticipate in advance how many elements you are going to store in the ArrayList. An ArrayList automatically expands as new items are added, making it ideal for scenarios where data size is unpredictable.

Internally, however, the ArrayList isn’t a magical "elastic" structure. It starts with an underlying array of a default capacity (10 elements). When this limit is exceeded, Java creates a new array that is 1.5 times larger than the original array and efficiently copies the existing elements into it. This resizing strategy ensures smooth performance while offering the flexibility developers love about the ArrayList in Java

Refer How does ArrayList work internally in Java to know more about how does ArrayList work internally in Java.


Hierarchy of the ArrayList

To know the hierarchy of java.util.ArrayList you need to know about 2 interfaces and 2 abstract classes.

  • Collection Interface- Collection interface is the core of the Collection Framework. It must be implemented by any class that defines a collection.
  • List interface- List interface extends Collection interface. Apart from extending all the methods of the Collection interface, List interface defines some methods of its own.
  • AbstractCollection- Abstract class which implements most of the methods of the Collection interface.
  • AbstractList- Abstract class which extends AbstractCollection and implements most of the List interface.

ArrayList extends AbstractList and implements List interface too. Apart from List interface, ArrayList also implements RandomAccess, Cloneable, java.io.Serializable interfaces.