Can you help me, why this code shows - "8"? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
15th Dec 2018, 9:05 AM
Rinat Muzafarov
Rinat Muzafarov - avatar
2 Answers
+ 1
Because the while loop run until x is greater then 7, so when x is 8 it will exit the loop, hence it will print 8! x = 0 loop 1 x = 0 -> x++ -> x = 1 loop 2 x = 1 -> x++ -> x = 2 and so on until loop 7 x = 6 -> x ++ -> x = 7 now you put the exit condition to x <= 7 so when x is 7 still will run the loop one more time that is: loop 8 x = 7 -> x++ -> x= 8 now x = 8 so x > 7 so exit loop and output of x is 8!
15th Dec 2018, 9:28 AM
Sekiro
Sekiro - avatar
+ 1
Because when it reach 7 The condition x<=7 still true, thus x++ expression still executed
15th Dec 2018, 9:28 AM
Taste
Taste - avatar