Reversing lines of output and assignemnt operator gets different answers?


#1

THIS:
i = 0
while i < 5
puts i
i+=1
end
yields:1 2 3 4

THIS:
i = 0
while i < 5
i+=1
puts i
end
yields:1 2 3 4 5

Why is the results different? Thank you for any help.


#2

Of course it can change the output, since you base your output on your variable i.

In the first code, you'll display before incrementing, so it will display from 0 to 4 included (because your i is initialized at 0).

In the second code, you'll increment first and THEN display, so when you reach your display method your i will always be 1 unit greater than in the first code.


#3

Thank you, that makes sense. I haven't been thinking much about the effects of order thus far.