Click here to Skip to main content
15,443,631 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Python
x = 0
while True: # outer loop
    while True:
        if x == 10:
            break
        print(x)
        x += 1


What I have tried:

Python
x = 0
while True:
    while True:
        if x == 10:
            break
            break
        print(x)
        x += 1
Posted
Updated 2-Oct-17 20:14pm

1 solution

Refactor your code: move both loops into a function, and replace the break with a return.
 
Share this answer
 
Comments
Zarif25 3-Oct-17 2:44am    
That didn't work. Is there any other way?
OriginalGriff 3-Oct-17 3:05am     CRLF
Yes it does - if you did it properly. What code did you use?
Zarif25 3-Oct-17 5:58am     CRLF
ow...my mistake. by the way, is there any other way to do it?
OriginalGriff 3-Oct-17 6:09am     CRLF
Not a good one: you can use a variable that the outer loop checks, but that's messy. Or there is goto - but that will get you strung up by the neck in many places - and quite rightly!
Zarif25 3-Oct-17 6:09am     CRLF
thnx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900