What is the % for in this script? It's the "if i % 2 == 0 then" part that confuses me.
1 | for i = 1 , 20 do |
2 |
3 | if i % 2 = = 0 then |
4 | print (i .. " even" ) |
5 | else |
6 | print (i .. " odd" ) |
7 | end |
8 | end |
It is the modulus operator.
It gets the remainder if you divide the first number by the second.
Examples:
1 | print ( 5 % 2 ) --> 1 (Because 5 / 2 = 2, remainder 1) |
2 | print ( 10 % 4 ) --> 2 (10 / 4 = 2, remainder 2) |