All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Break While Loop Python

Last Updated : Mar 11, 2024

Break While Loop Python

In this tutorial we will show you the solution of break while loop python, a programming structure that implements iteration is called a loop. Loops can execute a block of code a certain number of times until a certain condition is met. Their usage is fairly common in programming.

Loops iterate over a block of code, but sometimes we wish to terminate the current iteration or even the whole loop without checking the test expression. So, let’s get into it.

Step By Step Guide On Break While Loop Python :-

The while loop is used to iterate through the given code for an infinite number of times.

The while loop will keep on executing the given block of code until the given condition is true.

As the condition becomes false, the execution moves outside of the while loop, or Python also allows using the else statement as the condition becomes false.

x = 10
y = 10
z = 10
while x <= 100:
    if x == 30:
        break
    print("X =", x)
    x = x + 10
while z <= 100:
    while y <= 100:
        if y == 30:
            break
        print("Y =", y)
        y = y + 10
    if z == 30:
        break
    print("Z =", z)
    z = z + 10
  1. We will see how to break out of a while loop forcefully using two approaches: without a nested while loop and with a nested while loop.
  2. In the first three lines, there are three different variables named x, y, and z, which we will use throughout this article.
  3. X has a value of 10, Y has a value of 10, and the z variable also has a value of 10.
  4. In the next line, there is a while loop which checks the condition whether the value of the variable x is less than or equal to 100. If the condition is true, then the loop block will be executed, otherwise not.
  5. In the next line, there is an if condition that checks the value of the variable x, that whether it is 30 or not. If it is, then execute the break statement; otherwise, display the value of the variable x.
  6. The Python break statement immediately terminates a loop entirely. Program execution proceeds to the first statement after the loop body in the program.
  7. After the break statement, there is a print statement that will display the value of the variable x.
  8. In the next line, there is an expression that will increment the value of the variable x by 10 in each iteration.
  9. The print statement will display the value of the variable x. Here it will display only 10 and 20.
  10. In the next line, there is another while loop that has the iteration variable z. The condition for the while loop is the same as the above while loop.
  11. In the next line, there is a while loop as well, which is a nested while loop that will iterate with the help of the variable y.
  12. The process of execution of the while loop in this approach is similar to the above while loop, with the exception that this while loop is nested.
  13. If the condition is met, the break statement will be executed, forcing the inner while loop to terminate and passing control to the outer while loop to execute.
  14. Now here in the inner while loop, the print statement will display the value of the variable y, which is only 10 and 20.
  15. After that, the outer while loop will start to execute, and when the if condition from the outer loop is true, the break statement will execute, forcing the outer loop to end.
  16. The print statement from the outer while loop will display the value of the variable z in each iteration. Here it will display only 10 and 20.

Conclusion :-

So finally, in conclusion, we can say that with the help of this article, you can now forcefully break a while loop in a Python program.

You can use any of the methods mentioned above that you find easy to understand and all the methods will work fine.

I hope this tutorial on break while loop python helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪