Why sometimes print 0 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why sometimes print 0

In phyton Why 0 is printed ? i = 0 while True:   print(i)   i = i + 1   if i >= 5:     print("Breaking")     break print("Finished") Output 0 1 2 3 4 Breaking Finished Why 0 is is not printing and why 5 was printed? i = 0 while i<5:   i += 1   if i==3:     print("Skipping 3")     continue   print(i) Output 1 2 Skipping 3 4 5

2nd Dec 2022, 3:01 AM
William A Cruz Mendoza
William A Cruz Mendoza - avatar
2 Answers
+ 4
i = 0 while True:   print(i)   i = i + 1   if i >= 5:     print("Breaking")     break print("Finished") // Here you are printing i (which is 0) before incrementing it. That's why it will start from 0
2nd Dec 2022, 3:19 AM
JOKER
JOKER - avatar
+ 3
i = 0 while i<5:   i += 1   if i==3:     print("Skipping 3")     continue   print(i) Here you incremented the i value (which was 0 and now it's 1 after incrementing) before printing it, that's why you get 1 and not a 0
2nd Dec 2022, 3:20 AM
JOKER
JOKER - avatar