+ 1

Why this happened?

sumantan mukherjee Apr 17, 03:35 PDT When I'm writing- count = 0 while count <= 10:    count = count+1    if count % 2 == 0:       continue    print(count) In the third line, of the code I'm writing, count = count+1.But if I write this line at the end of the code it's not giving any output. While writing another one code like the code below-  count = 0 while count <15:    if count == 5:       break    print(count)    count += 1 I'm writing count = count+1 at the last of the code and yet getting output , why is this happening?why I'm not able to write count = count+1 at the last of the code in the 1st one set?Please answer me. And When I sit to solve a problem , I probably get confused by loops(in case of solving a problem).I can't understand when should I use 'while loop' and when should I use 'for loop'! So, please help me to overcome this one confusion.

18th Apr 2020, 3:44 AM
Sukanya Mukherjee
Sukanya Mukherjee - avatar
2 ответов
+ 4
The key factor here is not the position of 'count = count + 1', but rather the 'continue' statement. 'continue' just dimisses everything after it, and not before it. So in your case, when the counter%2==0, then everything after that conditional will not be executed. Thus if you write count=count+1 before it will run, but if you write it after then it won't.
18th Apr 2020, 3:52 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
Thank you for your valuable answer.
18th Apr 2020, 4:27 AM
Sukanya Mukherjee
Sukanya Mukherjee - avatar