Why is my code wrong?


i = 20
loop do
  i -= 1
   for  i in 1...19
  next if i % 2 == o
  print i
  print "#{i}"
 break if i <= 0

it's running forever. Help,please


What is the significance of i -= 1?

In order to be able to test your code (and read the instructions) we will need a link to the exercise. Please post it, thank you.


Exercise 9 Next!

"i - = 1" means i = i - 1


i alredy finish the exercise,but i dont understand why i can not use "for" inside the "loop"


I know what it means. I asked what its significance is?


If you are going to have nested loops then give them each their own control variable. You are using i in both loops, and it will never be less than 0 where your code is testing it. It just finished counting to 19.


ooh,so i can use "for" inside the "loop",without the "i -= 1"?


We may nest loops as needed, just so long as they have their own independent controls. Consider:

k = 20
loop do
    k -= 1
    for i in 1...19
        next if i % 2 == 0
        print i
    print "#{k}"
    break if k <= 0



what's "i" on the code? Their own control?


i is the control on the for loop. k is the control on the do loop.

Given scope constraints, the for loop can see k but the do loop cannot see i. The key is that each loop has a termination condition built in.


oh,man. Thank you very much!!


But how can i make the first code run,starting on 18,and stopping on 2? i don't get it