Simple way to decrement number within a repeating range I currently have a solution to increment numbers within a range:

i % x + 1


Where x is the maximum number in the range and i is the previous number.

It is nice and simple, and gives this array: 1,2,3,4,1,2...

I also need to do this in reverse, which should produce this array: 1,4,3,2,1,4,3...

This is my solution:

i == 1 ? x : (i - 1 % x)


Is there a way to do it without the ternary operator?
Some thoughts:

(i + x) % x == i //For i &gt;= 0 &amp;&amp; i &lt; x


Then

(i + x - 1) % x


Gives us the range from 0 to x-1.
We want the range from 1 to x so we change this to:

(i + x - 2) % x + 1;


But I don't think it's particularly readable, so your ternary code might end up being better.