Python break

Summary: in this tutorial, you’ll learn about the Python break statement and how to use it to exit a loop prematurely.

Introduction to the Python break statement

Sometimes, you want to terminate a for loop or a while loop prematurely regardless of the results of the conditional tests. In these cases, you can use the break statement:

breakCode language: Python (python)

Typically, you use the break statement with the if statement to terminate a loop when a condition is True.

Using Python break with for loop

The following shows how to use the break statement inside a for loop:

for index in range(n): # more code here if condition: breakCode language: Python (python)

In this syntax, if the condition evaluates to True, the break statement terminates the loop immediately. It won’t execute the remaining iterations.

This example shows how to use the break statement inside a for loop:

for index in range(0, 10): print(index) if index == 3: breakCode language: Python (python)

Output:

0 1 2 3Code language: Python (python)

How it works.

  • The for loop iterates over 10 numbers from 0 to 9 and displays each of them on the screen.
  • However, when the loop counter is 3, the break statement terminates the loop immediately. Therefore, the program shows only 4 numbers, from 0 to 3 on the screen.

When you use the break statement in a nested loop, it’ll terminate the innermost loop. For example:

for x in range(5): for y in range(5): # terminate the innermost loop if y > 1: break # show coordinates on the screen print(f"({x},{y})")Code language: Python (python)

Output:

(0,0) (0,1) (1,0) (1,1) (2,0) (2,1) (3,0) (3,1) (4,0) (4,1)Code language: Python (python)

This example uses two for loops to show the coordinates from (0,0) to (5,5) on the screen.

The break statement in the nested loop terminates the innermost loop when the y is greater than one.

Therefore, you only see the coordinates whose y values are zero and one.

Using Python break statement with a while loop

The following shows how to use the break statement inside the while loop:

while condition: # more code if condition: breakCode language: Python (python)

The following example uses the break statement inside a while loop.

It’ll prompt you for entering your favorite color. The program will stop once you enter quit:

print('-- Help: type quit to exit --') while True: color = input('Enter your favorite color:') if color.lower() == 'quit': breakCode language: Python (python)

Output:

-- Help: type quit to exit -- Enter your favorite color:red Enter your favorite color:green Enter your favorite color:blue Enter your favorite color:quitCode language: Python (python)

How it works.

  • The while True creates an indefinite loop.
  • Once you enter quit, the condition color.lower() == 'quit' evaluates True that executes the break statement to terminate the loop.
  • The color.lower() returns the color in lowercase so that you can enter QuitQUIT or quit to exit the program.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *