What is the % for in this script? It's the "if i % 2 == 0 then" part that confuses me.
for i = 1, 20 do
if i % 2 == 0 then
print(i .. " even")
else
print(i .. " odd")
end
end
What is the % for in this script? It's the "if i % 2 == 0 then" part that confuses me.
for i = 1, 20 do
if i % 2 == 0 then
print(i .. " even")
else
print(i .. " odd")
end
end
It is the modulus operator.
It gets the remainder if you divide the first number by the second.
Examples:
print(5 % 2) --> 1 (Because 5 / 2 = 2, remainder 1)
print(10 % 4) --> 2 (10 / 4 = 2, remainder 2)
This discussion is preserved as it was. New answers, comments, and votes are closed.
2 comments
Thanks.
Works on non-integers too: 5.1 % 0.2 is 0.1