Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is the % for in Lua Scripting?

Asked by 9 years ago

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

1 answer

Log in to vote
2
Answered by 9 years ago

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)
0
Thanks. notes4u99 65 — 9y
1
Works on non-integers too: 5.1 % 0.2 is 0.1 BlueTaslem 18071 — 9y
Ad

Answer this question