We often face a situation where we are required to exit a loop completely when an external condition triggers. Similarly, there may also arise a situation when we wish to skip a part of the loop and start the next execution. In order to handle these kinds of situations, Python offers us to break and continue statements in order to have good control of our loop. Thus, you will see that in Python, break and continue statements hold the power to alter the flow of a normal loop. Loops iterate over a block of code until the test expression is false. However, sometimes we want to terminate the current iteration or even the whole loop without having to check the test expression. Therefore, we make use of the both these statements in these cases.
What is the Break Statement?
Basically, we make use of this kind of statement for terminating the loop when a particular condition is met. After studying this in detail, you already know that we use a loop for iterating a set of statements repeatedly as long as the loop condition returns true.
Further, we use this statement usually inside a loop along with an if statement. Thus, when a certain condition returns true, the break statement will be encountered and consequently, the loop will terminate.
For instance, suppose that you are searching for an element in a list. Thus, for that purpose, you will be running a loop that starts from the first element of the list to the last element of the list.
By making use of this type of statement, one can terminate the loop as soon as the element is found. It is because why would we run the loop unnecessary till the end of the list when we have found our element.
In other words, one can achieve this through the help of this statement.
Browse more Topics Under Conditional Constructs and Looping
- If Else Statement While
- For (Range Function)
- Nested if
- Nested Loops
- Use of Compound Expression in Conditional and Looping Construct
All You Need to Know About Break
As you know by now, this kind of statement terminates the loop which contains it. Thus, the control of the program flows to the statement immediately after the body of the loop. Most importantly, if this statement is inside a nested loop, which is, loop inside another loop, this kind of statement will terminate the innermost loop.
Moreover, you need to remember that the break refers to a keyword in Python which comes in use for bringing the program control out of the loop. Further, this type of statement is responsible for breaking the loops one by one.
This happens in the case of nested loops. That is to say, it breaks the inner loop first and then moves on to the outer loops. Meaning to say, it comes into use for aborting the current execution of the program and the control goes to the next line after the loop.
Most importantly, it comes into use commonly in cases where one needs to break the loop for a certain condition.
Syntax of Break
The syntax of this kind of statement in Python is similar to what you see in Java. It is:
Flow Diagram of Break
# Use of this kind of statement inside the loop for val in "string": if val == "i": break print(val) print("The end")
s t r The end
In this program, one iterates through the “string” sequence. One check if the letter is I, upon which one breaks from the loop. Therefore, you see in your output that all the letters up till I will be printed. After that, the loop will terminate.
list =[1,2,3,4] count = 1; for i in list: if i == 4: print("item matched") count = count + 1; break print("found at",count,"location");
In this instance, we will be searching for the number ’88’ in the provided list of numbers. The requirement is to display all the numbers till the number ’88’ is found. Moreover, when it is found, terminate the loop and do not display the rest of the numbers.
# program to display all the elements before number 88 for num in [11, 9, 88, 10, 90, 3, 19]: print(num) if(num==88): print("The number 88 is found") print("Terminating the loop") break
11 9 88 The number 88 is found Terminating the loop Output: item matched found at 2 location
str = "python" for i in str: if i == 'o': break print(i);
p y t h
Example: Break statement with While Loop
i = 0; while 1: print(i," ",end=""), i=i+1; if i == 10: break; print("came out of while loop");
0 1 2 3 4 5 6 7 8 9 came out of while loop
Point to Remember
Please remember that one would always want to use this kind of statement with an if statement so that only when the condition linked with ‘if’ is true, then only the break gets encountered.
Moreover, if one does not use it with the ‘if’ statement then the break statement will be encountered in the first iteration of the loop. Thus, the loop will always terminate on the first iteration.
FAQ on Break
Question 1: What is the break statement in Python?
Answer 1: This type of statement basically terminates the loop containing it. Control of the program will flow to the statement immediately after the body of the loop. If this statement is inside a nested loop (loop inside another loop), this kind of statement will terminate the innermost loop.
Question 2: How do you insert a break in Python?
Answer 2: In order to insert a break in Python, make use of “\n” to print a line break <a name=”use-“\n””> Insert “\n” at the desired line break position.
Question 3: How does break continue and pass work in Python?
Answer 3: In Python, this type of statement comes into use for terminating the loop or statement in which it is present. After that, the control will pass to the statements that are present after the break statement, if available.
Question 4: What does pass do in Python?
Answer 4: The pass statement comes into use as a placeholder for future code. When the pass statement executes, nothing occurs. However, you do avoid getting an error when an empty code is not allowed. Moreover, empty code is not allowed in loops, function definitions, class definitions, or if statements.