Jump Statement in Python-Break Statement

Loops are a very helpful, time-saving and efficient at the same time. Using loops is our first choice if we want to automate and repeat a task. But, sometimes, we get into a situation where

  • We have to terminate the loop if a particular condition becomes true.
  • We have to terminate that particular iteration and continue with the next if the condition turns out to be true.

To control situations like this, we have the jump statements in python

These are called so because if we use these statements, the program's control takes an unconditional jump to another statement elsewhere in the code. These statements are:

  1. Break statement
  2. Continue statement
  3. Pass statement
  4. Return statement

In this article, we will focus on the first jump statement-Break

Definition:

The break statement is the loop-control statement used in Python to terminate a loop if a particular external condition turns out to be true.

  • As soon as the interpreter finds a break statement in the code, it terminates the loop and transfers the control to the immediately next statement to the loop.
  • This statement is almost always written in an if statement in the loop's body and is also used in switch-case statements to check a condition.

Syntax:

Flowchart:

Jump Statement in Python-Break Statement

If the test expression or the condition to enter the loop becomes true, we enter the loop. Inside the loop, if the compiler comes across the break statement, the execution of the loop is terminated.

The succeeding statement after the loop is executed, else the remaining body of the loop is executed.

Working of the break statement in for and while loops:

Jump Statement in Python-Break Statement

Important points:

  1. We cannot use a break statement without a loop. It raises an error.
    "SyntaxError:'break' outside loop".
  2. We can use break in switch-case statements.
  3. If the break statement is used in a nested loop inside the inner loop, then the inner loop will be terminated.
  4. "break" is a reserved keyword in python.
  5. We cannot use labels, conditions, or other options with the break statement in python.

Example situation:

Imagine you have to calculate the sum of numbers given by the user, and if the user inputs a negative number, the calculation must stop, and it should print the sum till the last number. If the user wants to stop the calculation for numbers before the max limit, he can give a negative input.

Output:

Enter a number: 1

Enter a number: 2

Enter a number: 3

Enter a number: 4

Enter a number: 5

Enter a number: 6

Enter a number: 7
Enter a number: 8
Enter a number: 9
Enter a number: -9
The sum of the 9 numbers you gave:  45

Understanding:

This program is designed to find the sum of the positive number given max limit: 100. Inside the loop, we check if the input given by the user is greater than 0. If not, the break statement is executed, the loop is terminated, and the next statement is executed, i.e., the print statement. Else, we add the number to the sum.

You may wonder:

What if the user wants to give more numbers to calculate the sum, and the negative number is just a typo and we need to skip negative numbers? We have the 'continue' statement for such types of applications.

Example programs:

Output:

Enter the number you want to search: 56
That is a match
The item 56 is found in the index 1

Output:

Enter the number you want to search: 34
The element 34 is found at 0 index

Output:

Found it 1
Found it 2
Found it 5
Found it 6

Understanding:

We are trying to print the tuples' values that don't have 3 in them. In the outer loop, we are iterating the tuples in the list, and in the inner loop, we are iterating the values in the tuple. When checked in the first tuple, 3 is not found. So, the elements are printed. In the next tuple, 3 is found. It encountered a break statement for the entire 2nd tuple in the loop. As a result, the inner loop got terminated, and the outer loop iterated to the next iteration with the third tuple.

Labeled Break statement:

Just as the name suggests, a labeled break statement will have a label attached to it on what to break.

We can break the outer loop using the labeled break instead of only the inner loop. But, this is not available in our python language. Other popular programming languages use this mechanism, but it is not allowed in python.

A PEP is raised to the python community to add this labeled break statement in the language. Still, it is denied stating it adds unnecessary complexity to the code and the language.

If we want to use the functionality of this labeled break statement in python, we can move the whole code to a function and return the value.






Latest Courses