+ 2

I have a question about while loop

count = 0 while count < 7 print (count) count = count + 1 Output: 0 1 2 3 4 5 6 Why does it print 0 as well? Does it print the count when it's 0 first then proceeds to print the new count ?

27th Apr 2020, 4:02 PM
Einstein Shilly shally
Einstein Shilly shally - avatar
5 Answers
+ 4
You defined count as 0. You then printed count. Then added 1 and printed count each time you did until you hit 7. If you would have defined count as 3, youd get: 3 4 5 6
27th Apr 2020, 4:06 PM
Slick
Slick - avatar
+ 5
Why did you print count first and then adding 1 with the initial value?
29th Apr 2020, 12:29 PM
Mehnaz ✨
Mehnaz ✨ - avatar
+ 4
It prints 0 because you are printing before incrementing count ,so obviously it's initial value which is zero is printed first
27th Apr 2020, 4:05 PM
Abhay
Abhay - avatar
+ 3
You increase count with 'count = count + 1' AFTER you print its value with 'print (count)'
27th Apr 2020, 4:06 PM
Uros Zivkovic
Uros Zivkovic - avatar
+ 3
Look at your code carefully ... When it is printing the 1st time the value of count it is 0 cause after that the value is increasing by 1...so obviously it will be 0.... If you dont want to print 0 then write the count = count + 1 statement before the print statement or set the value of count 1.... . Happy Coding mam
27th Apr 2020, 4:18 PM
Md. Mursalatul Islam Pallob
Md. Mursalatul Islam Pallob - avatar