Nataly J
Nataly J

Reputation: 45

How can i use modulus the loop through integers -10 to 10?

I understand that i can use modulus to loop through positive numbers e.g 0 to 10 would look something like this:

i++;
i %= 10;

But how can i make it so instead of starting back at 0 it starts at -10? Thanks.

Upvotes: 0

Views: 78

Answers (2)

Svea
Svea

Reputation: 257

Could this be the solution?

i = i%20 - 10;

Upvotes: 1

Colin vH
Colin vH

Reputation: 537

Don't use modulus.

i++;
if (i >= 10) i = -10;

Upvotes: 1

Related Questions