The Python break Statement is an important one used to alter the flow of a program. We want to share two examples to display the working functionality of the Break statement in both For loop and the While loop.
Loops execute a specific code block for n number of times until the test condition is false. There will be some situations where we have to terminate the loop without executing all the lines. In these situations, we can use this Python break statement to terminate the for or while loop.
The Python break statement is beneficial to exit the control from For, While, and Nested Loops. While executing these code blocks, if the compiler finds this inside, it will stop running the code inside it and exit immediately from the iteration. Its syntax is
For example, we have 5 lines of code inside the loop, and we want to exit from it when a certain condition is True; otherwise, it has to execute them. We can place the Python Break statement inside the If condition in these situations.
If the condition is True, then the compiler will execute this statement. It means the break Statement will completely exit the controller from the iteration. Otherwise, it will run all blocks of code.
Python break Statement in For and While loop
This program will use the break in For Loop to exit from the block iteration.
In this break statement in for loop example, First, we used the for with range(), and we are not going to explain the iteration-wise execution here.
Inside the for loop, we placed the If condition to test whether i is equal to 6. If the condition is false, it will skip and print that number as output. In Our case, 0, 1, 2, 3, 4, 5 using the print function.
If this condition ( i == 6) is true, then this statement will execute, and the iteration will stop at that number without printing the following print function:
for i in range(0, 11): if i == 6: break print("The Value of the Variable i is: ", i)
This program will use the Python break statement inside the While loop to exit from the iteration.
In this example, We initialized the value of i as: i = 0 at the beginning of the code. Then, within this, we check for the condition whether i is less than or equal to 10 or not. Finally, inside the While loop, we placed the If condition to test whether i is equal to 4.
If the condition is false, it will skip this iteration. Next, it prints that number as output (In Our case, 0, 1, 2, 3) using the following print function.
If this condition (i == 4) is True, then the break one will execute, and the iteration will stop at that number without printing the following print function.
We also used Arithmetic Operators + operator to increment the i value (i = i +1). If you forget this line, you will end up in infinite iterations. Please refer to continue, For, and While in Python.
i = 0 while i <= 10: print(" The Value of the Variable i = ", i) i = i + 1 if i == 4: break
The Value of the Variable i = 0 The Value of the Variable i = 1 The Value of the Variable i = 2 The Value of the Variable i = 3