Until the inner loop "returns", the condition in the outer loop will never be re-examined. If you need this check to happen every time after i changes, do this instead:

while i<=10:
    for a in xrange(1, x+1):
        print "ok"
        i+=1
        if i > 10:
            break

That break will only exit the inner loop, but since the outer loop condition will evaluate to False, it will exit that too.

Answer from mgibsonbr on Stack Overflow
🌐
Programiz
programiz.com › python-programming › break-continue
Python break and continue (With Examples)
i = 0 while i < 5: if i == 3: break ... flow of the program goes to the next iteration. ... We can use the continue statement with the for loop to skip the current iteration of the loop and jump to the next iteration....
🌐
Reddit
reddit.com › r/learnpython › how can i break out of a while loop immediately and at any point during the while loop.
r/learnpython on Reddit: How can I break out of a while loop immediately and at any point during the while loop.
June 23, 2023 -

I have a long while loop, it has a lot of code in it and a lot of delays.

I want to break out of the loop when a condition is met but I need to break out of it instantly. The only thing I can think of, is to print if statements constantly through the loop, checking if the condition is true, but this seems a little silly because it would have literally 100s of break statements.

Is there any better way to do this without typing break statements after every single line of code, that seems impractical because there's 100s of lines of code in the while loop, it takes around 10 minutes to finish and I need it to break instantly

Discussions

How to break out of while loop in Python? - Stack Overflow
I have to make this game for my comp class, and I can't figure out how how break out of this loop. I have to play against the "computer," by rolling bigger numbers, and seeing who has the More on stackoverflow.com
🌐 stackoverflow.com
How do I end a For or While loop without using break?
A for loop ends when it exhausts its iterable, and a while loop terminates when its condition is no longer met. If you were going to write while True: if some_variable == "some value": break #do stuff then instead you could write: while some_variable != "some value": #do stuff More on reddit.com
🌐 r/learnpython
4
4
March 19, 2022
Use of break in Python
Break is a valid move. More on reddit.com
🌐 r/learnpython
14
0
May 11, 2019
Using break statement to end a loop.

Your code works fine, but you should be putting the check for if the user input is quit before the print statement. It should look like below.

while True: 
  user_input = input("Write something") 
  if user_input == "quit": 
    break 
  print(user_input, end = " ") 
print("Bye bye")
More on reddit.com
🌐 r/learnpython
8
4
June 19, 2018
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-break-statement
Python Break Statement - GeeksforGeeks
The break statement in Python is used to immediately terminate a for or while loop when a specified condition is met.
Published   June 5, 2026
🌐
W3Schools
w3schools.com › python › python_while_loops.asp
Python While Loops
The while loop requires relevant variables to be ready, in this example we need to define an indexing variable, i, which we set to 1. With the break statement we can stop the loop even if the while condition is true:
🌐
Mimo
mimo.org › glossary › python › break-statement
Python Break Statement: Syntax, Usage, and Practical Examples
The break statement in Python is used to immediately exit a loop—either a for loop or a while loop—before the loop has iterated over all its items or reached its ending condition.
🌐
Real Python
realpython.com › python-while-loop
Python while Loops: Repeating Tasks Conditionally – Real Python
March 3, 2025 - You’ve learned how to use while loops to repeat tasks until a condition is met, how to tweak loops with break and continue statements, and how to prevent or write infinite loops. Understanding while loops is essential for Python developers, as they let you handle tasks that require repeated execution based on conditions.
Find elsewhere
🌐
Coursera
coursera.org › tutorials › python-break
How to Use Python Break | Coursera
When working with loops, remember that indentation tells Python which statements are inside the loop and which are outside the loop. Your break statement should follow your if statement and be indented.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-use-break-continue-and-pass-statements-when-working-with-loops-in-python-3
How To Use break, continue, and pass Statements in Python | DigitalOcean
April 24, 2026 - Python allows an else clause to be used with both for and while loops. While this feature may seem unusual at first, it can lead to cleaner and more readable code, especially when combined with control flow statements like break.
🌐
LearnDataSci
learndatasci.com › solutions › python-break
Python break statement: break for loops and while loops – LearnDataSci
For any strings that contain an i, break exits our for char in string: loop. As this is our inner-most loop, Python then moves onto the next item in the for string in strings: loop. It's worth noting that if Python doesn't terminate while loops, they can loop endlessly.
🌐
Problem Solving with Python
problemsolvingwithpython.com › 09-Loops › 09.03-Break-and-Continue
Break and Continue - Problem Solving with Python
An example using break in a for loop is below. ... When the loop hits i=3, break is encountered and the program exits the loop. An example using break in a while loop is below. ... In Python, the keyword continue causes the program to stop running code in a loop and start back at the top of the loop.
🌐
Tutorialspoint
tutorialspoint.com › python › python_loop_control.htm
Python Break, Continue and Pass Statements
for num in range(10,20): #to iterate between 10 to 20 for i in range(2,num): #to iterate on the factors of the number if num%i == 0: #to determine the first factor j=num/i #to calculate the second factor print ('%d equals %d * %d' % (num,i,j)) break #to move to the next number, the #first FOR else: # else part of the loop print (num, 'is a prime number') ... 10 equals 2 * 5 11 is a prime number 12 equals 2 * 6 13 is a prime number 14 equals 2 * 7 15 equals 3 * 5 16 equals 2 * 8 17 is a prime number 18 equals 2 * 9 19 is a prime number · Similar way you can use else statement with while loop. The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.
🌐
Enki
enki.com › post › how-to-exit-a-loop-in-python
Enki | Blog - How to Exit a Loop in Python
Learn to manage loops effectively in Python using the break statement to exit for and while loops under specific conditions, optimizing code efficiency.
🌐
Learn Python
learnpython.dev › 02-introduction-to-python › 110-control-statements-looping › 40-break-continue
break, continue, and return :: Learn Python by Nina Zakharenko
They’re a concept that beginners to Python tend to misunderstand, so pay careful attention. The break statement will completely break out of the current loop, meaning it won’t run any more of the statements contained inside of it. >>> names = ["Rose", "Max", "Nina", "Phillip"] >>> for name ...
🌐
freeCodeCamp
freecodecamp.org › news › python-break-and-python-continue-how-to-skip-to-the-next-function
Python Break and Python Continue – How to Skip to the Next Function
March 14, 2022 - We could add a condition inside our while loop that says if num is 9, then break out of the loop. num = 5 while num < 20: print('Current number :', num) num = num + 1 if num == 9: break ...
🌐
TutorialKart
tutorialkart.com › python › python-while-loop › python-while-loop-break
Python While Loop with Break - Examples
October 7, 2024 - Python While Loop executes a set of statements in a loop based on a condition. But, in addition to the standard breaking of loop when this while condition evaluates to false, you can also break the while loop using builtin Python break statement.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Python while Loop (Infinite Loop, break, continue) | note.nkmk.me
August 18, 2023 - Infinite loops with counters and ... can specify multiple conditions for the condition part with and or or. ... Use break to break a while loop....
🌐
freeCodeCamp
freecodecamp.org › news › python-break-statement-tutorial
Python Break Statement – How to Break Out of a For Loop in Python
April 16, 2024 - So instead of printing the whole list ("Jade", "John", "Jane", "Doe"), "Jade" and "John" were printed because the loop stopped immediately after it found "John". You can terminate a while loop using the break statement: