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
2 Antworten
+ 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
+ 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