MATLAB: While loop going one too far

relational operatorswhile loop

While learning to use while loops, I came across this comparison between a for and while loop.
%%For loop
for i = 1:0.2:2
y = i^2;
end
%%While loop
i = 1;
while i < 2
y = i^2;
i = i + 0.2;
end
However, these two loops don't evaluate to the same thing. The for loop has i reaching 2, but the while loop has an ending i value of 2.2, which means when i=2, it still entered the while loop despite 2 < 2 being false. How is this possible? Shouldn't i exit when i=2 since 2<2 returns false?

Best Answer

Seems like the same problem as here .