Why is my code wrong?


#1

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

it's running forever. Help,please


#2

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.


#3

Exercise 9 Next!

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


#4

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


#5

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


#6

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.


#7

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


#8

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
    end 
    print "#{k}"
    break if k <= 0
end

`


#9

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


#10

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.


#11

oh,man. Thank you very much!!


#12

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


#13

http://ruby-doc.org/core-2.0.0/Numeric.html#method-i-step

http://ruby-doc.org/core-2.2.0/Integer.html#downto