Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1
This is quite simple, its because of the order in which the code is written In the first case, num starts from 1, and so the first number printed is 1. After each repetition of the loop, num is increased at the end, and each of the new values are checked, then DIRECTLY printed. When num=6, the condition (num < 6), is false and the loop ends and 5 is the last number. When you change the order, num starts with 1, but it is increased before printing, so the first number is 2. Each time the loop starts over, the number is checked, then INCREASED, THEN PRINTED. So if 2 is checked, 3 is printed, 3 is checked and 4 is printed. So when num=5, the condition (num<6) is true, so it is increased to 6, and then printed. So 6 is the last number. If you still have a doubt, try going over the code step-by-step or try it with a smaller number like 3.
21st Mar 2018, 2:03 PM
Just A Rather Ridiculously Long Username