Introduction to Python Break Statement
The Python Break Statement is one of the Loop control statements which is used to change the normal flow of execution. When the flow is redirected, the created objects in that flow get destroyed. Python supports below loop control statements:
- Break: Terminated the flow of the loop statement and executes the next statement outside the loop.
- Continue: It is used when we need to skip the execution of the remainder of statements in the loop and continue from the start.
- Pass: It is used when we need some statements syntactically, but do not want to put any statements.
This statement is used to terminate the flow of execution of loops in a program. The flow can also be diverted to different statements or different loop. A break statement can be used in the situation where we need to exit a loop (while as well as for loop)when one of the events get triggered. When a BREAK statement gets triggered in an inner loop, the flow of execution if diverted to the next statement in the outer loop.
Understanding Python Break Statement
While entering the loop a particular condition is being checked. If it satisfies statements in the loop are executed. But in case it does not get fulfilled in that case loop gets broken and flow is redirected to the next statement outside the loop. Here break statement is used to break the flow of the loop in case any trigger occurs other than the stopping condition occurs.
How do you Write a Break Statement?
The Break Statement is a simple statement that holds the power of terminating the normal flow of the group of statements. Let us think of a simple scenario of a game say, subway surfer. Its working is not that much difficult. When one person starts the game it enters a long and never-ending infinite loop. While moving in a loop the objects being displayed are continuously changing. On every stage, it takes your input according to the objects being displayed eg – if currently, a bridge comes which you need to cross. Now every move you will make will be checked against various conditions or checks that have been applied .in the game.
In all these checks there is also one crucial check which decides if u r still in the game or not.eg –u need to jump and u don’t. Then the quit condition fails and the loop gets broken and u r out of the game. This quitting work is done by the ‘break’ statement. It becomes active when the check or condition mentioned becomes true.
For writing a break statement. Following things are needed:
1. Quit or End condition: It is also known as hault condition which must be satisfied for the execution of break statement.
4.8 (3,227 ratings)
for I in a;
print ‘Found a number in the list’
Explanation: The above code prints all the alphabets present on the list. In case it finds the first numeric in the list, break statement gets executed and break the loop and print ‘Found a number in the list’ ‘
2. The loop: The break statement is always used in a loop. It is meaningless in case we don’t use any loop Because it is meant to break the repeated sequence of the statement, which is only present in a loop.
Syntax of Python Break Statement
Break can be written in the below format.
We need to just write ‘break’ after the condition that should be true to terminate the flow of execution.
Flow Diagram of Python Break Statement
Below is the flow of how the break statement works in a program.
The program continues with the normal execution of the program. In case it faces a conditional code while executing a loop, and the output of the condition is true, then the flow redirected from its normal flow to the first next statement after the loop. In case the condition happens to be false, the loop does execute in the same manner.
Examples in Python Break Statement
Python allows below loops for executing Iterative statements in a program
1. WHILE LOOP
The while loop executes the group of statements in sequence continuously until a stop condition is not fulfilled.
Eg: In the below program, there is an infinite loop that gets triggered always as while(1) is an always true condition. But we have declared a break statement which will be executed when the condition given in the “IF” statement will be true. And after that the just next statement after the loop will get executed.
Print “We are in the loop”
print ”Here break statement works”
print “I am out of the loop”
2. FOR LOOP
A FOR loop is used to iterate over a sequence like range, list, tuple, dictionary or string.
For I in [sequence]:
for I in a:
print “We are in loop with”
print ”Here Break statement is triggered”
print ”We are outside the loop”
The break statement is a loop control statement that terminates the normal execution of a sequence of statements in a loop and passes it to the next statement after the current loop exits. This can be used in many loops – for, while and all kinds of nested loop.
This is a guide to Python Break Statement. Here we have discussed Understanding, How do you Write a Break Statement, 2 Examples with codes and outputs. You can also go through our given articles to learn more –