+ 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

11th Aug 2020, 1:25 PM
Abhay
Abhay - avatar
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
11th Aug 2020, 1:51 PM
Alexander Thiem
Alexander Thiem - avatar
+ 2
As Alexander Thiem said move the if break statement left, as I shown in code
11th Aug 2020, 2:22 PM
v@msi😉
v@msi😉 - avatar
+ 1
11th Aug 2020, 1:52 PM
v@msi😉
v@msi😉 - avatar
+ 1
Abhay Did you understand my explanation?
11th Aug 2020, 2:03 PM
Alexander Thiem
Alexander Thiem - avatar
+ 1
Abhay I added a ecplanatory code!
11th Aug 2020, 2:18 PM
Alexander Thiem
Alexander Thiem - avatar
0
v@msi😏😏 I am getting right output as I said when if break is used before extend array
11th Aug 2020, 2:02 PM
Abhay
Abhay - avatar
0
Alexander Thiem I will definitely respond back to you ,I am still trying to understand your explanation
11th Aug 2020, 2:11 PM
Abhay
Abhay - avatar