Why it skips number 2 in the first code. And it doesn't skip number 40 in the second code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why it skips number 2 in the first code. And it doesn't skip number 40 in the second code

First code: i = 0 while True: i = i +1 if i == 2: print("Skipping 2") continue if i == 5: print("Breaking") break print(i) print("Finished") Second code: i=10 while True: print(i) i+=10 if i==40: print("holly") continue if i>70: break

15th May 2020, 10:12 PM
Hiba Y.S.M
Hiba Y.S.M - avatar
4 Answers
+ 3
please mention the language name is tags In first code first, 1 is printed Then when i==2 It prints skipping 2 and throws the control back to while loop now i is 3 and gets printed In second code After printing 10,20,30 when i reaches 40 it prints holly and throws the control back to while loop ,now after while loop it says print(i) which is 40 now so 40 gets printed and then it's value is increased and now i is 50 If i+=10 was before print(i) 40 wudn't get printed and 50 would be printed after holly
15th May 2020, 10:24 PM
Abhay
Abhay - avatar
+ 4
Because continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop does not terminate but continues on with the next iteration.
16th May 2020, 1:58 PM
vikas vareya
vikas vareya - avatar
+ 1
the condition of number 40 is reached but you always print at the start of the while body so 40 is printed
15th May 2020, 10:25 PM
Jnn
Jnn - avatar
+ 1
After "continue" nothing would happen even if "continue" is not called (except the last condition to break the loop)
15th May 2020, 10:27 PM
Jnn
Jnn - avatar