# Ruby modulo problem

#1

Can someone explain me why it prints 1,3,5?

prints 1,3,5

``````for i in 1..5
next if i % 2 == 0
print "#{i}\n"
end``````

#2

It prints `1,3,5` because after the loop 2 and 4 are factors of `2` so they are removed and the reminder is the non factors which is `1,3,5`

#3

I dont know what you mean. The code says if a f.e. 1 modulo 2 is 0 then print 1.
But 1 modulo 2 isn't 0 WTF

#4

@blogpro31841, in other words modulo '% 2' means 'every second'

#5

Isnt that a bullsh*t, the code says every second which equals zero?

#6

It's common code to checks if 'i' from your code is odd or even

#7

you don't say mr. sherlock...

#8

Attention! == never mean equal ! It is an operator of comparison. Here it compares as your every second 'i' is divisible by 2.

#9

There are a lot of stuff on it. The good source is: http://www.tutorialspoint.com/ruby/ruby_operators.htm

#10

Pls, correct me if i % 2 == 0 is not the same as i % 2 != 1 also.

#11

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