Using next if in a loop


for i in 1…5
next if i % 2 == 0
print i

This will produce all odd numbers 1,3,5, but my question is why? As I understand it, the loop reads as follows “If i divided by 2 produces no remainder, then put i”. From this logic, this if loop should produce all the even numbers rather than the odd numbers, as all even numbers satisfy the no remainder expression. Since it says to put i if it has no remainder, it should put even numbers.

This is not the case, why?



Think about the expression 'next if’
Think about next as “skipping” something. If it is divisible by two go ahead and skip it, print everything else.
Like the old MTV show ‘Next’


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