Use of i %3


#1

I am missing how i%3 is used in the continue example on page 53

if (i%3 ==0)

I understand that i%3 must resolve to 0 but don’t understand how the %operator works in this example.

I also see the using i%5==0 finds numbers divisible by 5 in the challenge.

Could someone explain what this does? It is different from using %d as a token to format a number but I’m not sure how.

thanks


#2

% is called the modulus or “mod” operator. It give the remainders from division.

So, 7 % 5 = 2. (Because 7 / 5 = 1 with a remainder of 2)

10 % 3 = 1 (Because 10 / 3 = 3 with a remainder of 1)

So i % 3 == 0 if and only if i is a multiple of 3.