0

Help needed coders...here its saying 'break' outside the loop...wat ds that mean

x = 0 while True : print(x) x+=2 if x>=14 : break

27th Jun 2018, 4:53 AM
Y AD Ù
Y AD Ù - avatar
10 Antworten
+ 5
Y Åđ Û The break statement ends execution of the nearest enclosing loop or conditional statement in which it appears. so it basically tells the computer this is the end of my loop or condition break;
27th Jun 2018, 5:04 AM
Agent
Agent - avatar
+ 3
thanks Pulkit Kamboj ..got an idea
27th Jun 2018, 5:33 AM
Y AD Ù
Y AD Ù - avatar
+ 2
the print statement position doesn't matter at all you shift the break statement in side the loop like this https://code.sololearn.com/cUS9Mm595kaE/?ref=app
27th Jun 2018, 5:16 AM
Pulkit Kamboj
Pulkit Kamboj - avatar
+ 2
thanks Pulkit Kamboj ..but bro...can i kwn y...we shld move break frm the previous postion..jst below the if statement itself is the break statement...and if the if statememt was true..then it shld break na...y did we add more spaces??
27th Jun 2018, 5:21 AM
Y AD Ù
Y AD Ù - avatar
+ 2
well it's really tough to explain in some lines but I will say that in Python there are no brackets so indentation(spaces) has a very I important role so in my case I had put the if statement inside the loop(by giving spaces ) but you have not given any spaces inside the if so if is outside the while loop, so in your case the if will only be executed when the while loop will stop and the loop is an infinite loop so it never stops but in my case the if statement is always executed as it's inside while loop.
27th Jun 2018, 5:32 AM
Pulkit Kamboj
Pulkit Kamboj - avatar
+ 1
well if you use break outside a loop in python it will surely raise an error because it is used to terminate a loop and has no purpose outside a loop.
27th Jun 2018, 5:05 AM
Pulkit Kamboj
Pulkit Kamboj - avatar
+ 1
In the code you have written as the if statement is outside the loop so the break is also outside the loop.In order to correct it you have to put the if inside the loop and that's exactly what I have done.You are making mistake in the indentation. Hope you have got it.
27th Jun 2018, 5:19 AM
Pulkit Kamboj
Pulkit Kamboj - avatar
0
even wen i gave a print statement after break....it still shows...break outside loop :( Pulkit Kamboj
27th Jun 2018, 5:09 AM
Y AD Ù
Y AD Ù - avatar
0
x = 0 while True : print(x) x+=2 if x>=14 : print("breaking") break print("k")
27th Jun 2018, 5:13 AM
Y AD Ù
Y AD Ù - avatar
0
still showing the same Pulkit Kamboj
27th Jun 2018, 5:13 AM
Y AD Ù
Y AD Ù - avatar