Code is not running

#1

When the run button is clicked, the button runs and then an error message to kill or wait pops up

``` i = 50 while i > 0 do print i i += 1 end

#2

Infinite loop. `i` should be getting smaller, not larger.

Use a while loop to print out the numbers 1 through 50, inclusive. While the example above counts down, you will want to count up.Use print rather than puts, and don’t forget to increment your variable.

So your code is correct but for the starting of value of `i` which should be `1`, not `50`, and the upper bound, which should be 50, not 1. Oh, and we will be comparing in the opposite direction.

``````i = 0
while i < 50 do
i += 1
print i
end
``````

Above, we started with `i` at zero, then incremented it immediately inside the loop. There are two consequences,

1. 1 is printed first
2. 50 is printed last

If we started with `i` at 1, and incremented after printing, the number 50 would not be printed unless we increased the upper bound to 51 (or included equality in the condition).

It takes a minute to wrap one’s head around these sort of counting loops, so indulge yourself and play with these loops a moment.

#3

Thank you so much for the explanation! After doing the exercise using the for loop like so:

for i in 0…49
i += 1
print i
end

I was a little confused as to how to apply the same logic to the while loop.

#4

We let the iterator handle the range in a for loop…

``````for i in 1..50
print i
end
``````

No clever tricks needed.

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.