+ 2
How does break keyword work in the following code?
If you place if and break statement after arr.extend then looping continues but if you place it before arr.extend then it works as expected ,so can anybody explain what is actually happening ?Thank you in advance! https://code.sololearn.com/cct2L4hDZdg9/?ref=app
7 Respuestas
+ 3
I think break justs breaks the second for slope....
1.array.extend before the break:
As the first for sloop never breaks every i is gone through
The moment where sum(array) equals n only the second for sloope is broken...
2. array.extend after the break.
Here i iterates through complete range too, but if the break is reached the array.extend funcion is broken too.....
for i in range(3):
print(i)
=>0 1 2
for i in range(3):
break
print(i)
=>
what you want is to stop both for loops! So you have to move the if and the break statement one block to the left!
Moreover, as there is only one j matching each i you can directly break the first foor-loop if i*j==n
https://code.sololearn.com/czW7iptePmnw/?ref=app
https://code.sololearn.com/colDS46UMfov/?ref=app
+ 2
As Alexander Thiem said move the if break statement left, as I shown in code
+ 1
Is this output you expecting?
https://code.sololearn.com/cxFnFv3ISqD3/?ref=app
+ 1
Abhay Did you understand my explanation?
+ 1
Abhay I added a ecplanatory code!
0
v@msi😏😏 I am getting right output as I said when if break is used before extend array
0
Alexander Thiem I will definitely respond back to you ,I am still trying to understand your explanation