FAQ: Loops & Iterators - Danger: Infinite Loops!

Having lots of bugs with the Ruby course! The Donald Duck exercise errored unless i clicked on the codecademy solution (which was the same as mine) and used that.

Same on this exercise but different error. My code (the same as codecademy’s) gave an infinite loop! I then had to exit, come back in and use codecademy’s same solution to get it to work.

My code:

i = 0
while i < 5
  puts i
  # Add your code here!
  i = i + 1

Any ideas?
Using chrome Version 87.0.4280.88 (Official Build) (x86_64)

I can’t understand why the code would turn out these weird results. Is bad arithmetic just something to expect from Ruby?

Not bad arithmetic, just binary arithmetic not able to exactly resolve decimal fractions. It’s complex, and hard to understand, but it traces back to floating point arithmetic.

Whether adding, multiplying, subtracting or dividing there is always the chance this minuscule error will surface. There are several ways to mitigate this which may come up in the course (or in your reading).

1 Like

i = 0

while i < 5:

Add your code here!

i = i + 1