0
How can I break 1st while loop using break statement after 2nd and 3rd while loop?
i=int(input("Enter first number: \n")) j=int(input("Enter second number: \n")) a=True while a==True: if i==j: print("Both numbers are equal") break elif i<j: while i<=j: print(i) i=i+1 if i>j: a=False else: while i>=j: print(i) i=i-1 if i<j: a=False https://code.sololearn.com/cTpR2peF2sbL/?ref=app
5 Respuestas
+ 2
No, you can't break an outer loop with a break, break only works in the loop you're in, so even if you try something like:
while True:
#your code here
you must declare a variable for break:
while True:
a = True
#your code and loops
if a==False:
break
But this is bad, because you're declaring the variable on each loop, just a waste of declarations. The way you did it is good
+ 1
Insert a break statement in the sub-loops, so the 2nd or 3rd loop will break, and since 'a' is no longer equal to 1, 1st loop will break too. And it's better to make it a=True instead of 1, and break with a=False instead of 2, because it can be very confusing in long codes
+ 1
Is it possible to break the first while loop using break statement insted of using a?
+ 1
Thank you for your help.