Jump Statement in Python-Break StatementLoops 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
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:
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.
Syntax: Flowchart: 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: Important points:
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. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India