why is there a 6? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why is there a 6?

i = 0 while i < 6: i += 1 if i == 3: continue print(i) 1 2 4 5 6

10th Mar 2024, 5:27 PM
planomics
3 Answers
+ 3
i is increased before you print it out. swap i += 1 and the print(i) and observe.
10th Mar 2024, 5:32 PM
Lisa
Lisa - avatar
+ 3
The while loop condition is tested only at the beginning of each loop. If the condition is true, then the loop statements are executed. Then, after the statements are executed, execution returns to the top of the loop and it checks again.
10th Mar 2024, 5:45 PM
Brian
Brian - avatar
+ 1
Thanks, everybody. I made a mistake when I did the math in my head. But pen and paper helped me realize
10th Mar 2024, 5:48 PM
planomics