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

91 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

Don't use modulus.

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

Could this be the solution?

i = i%20 - 10;