The condition in the
for
loop is only checked before each iteration, never while the actual body of the loop is running.
So in your case, in the second-to-last iteration of your loop, the program prints
which indicates that at this point,
x = 2178309
and
y = 3524578
. Now, the loop body ends and
d gets set to
y (which is 3524578). Since 3524578 is still less than or equal to 4 million, the loop runs again, which is why you get the two extra values.
Perhaps a simpler explanation is this: the loop stops
after the limit has been exceeded, not right before it's about to be exceeded.
You'll probably want to check
inside your loop to make sure you're not running over the limit.