<p>I've got a 1-based array of four items which I need to traverse. For a given index in the array, I need to work out the index of the next item, and the index of the previous item. This sounds simple but the array needs to wrap around. So when you are at the beginning of the array, the previous item is considered to be the last item. Likewise, if you are at the end of the array, the next item is considered to be the first item.</p>

<p>I realise this can be solved by using conditional statements, but I wondered if it was possible to do it using mathematical equations. I've got one that works nicely for the next item</p>

<pre><code>NextItem = Modulus(CurrentItem, 4) + 1
</code></pre>

<p>(the modulus function returns the remainder of one number divided by another).</p>

<p>Has anyone got any ideas as to how I can work out the previous item?</p>

Since modulo always works better on 0-based ranges, you can turn it into one of these using
```
CurrentItem - 1
```
.
Then, with modulo 4, adding 3 is the same as subtracting 1 but has the advantage of never going negative (some modulo operators may not like negatives).
Then you once again add 1 to get back to a 1-based number.
This gives:
```
```
PrevItem = Modulus(CurrentItem - 1 + 3, 4) + 1
```
```
or, simplified:
```
```
PrevItem = Modulus(CurrentItem + 2, 4) + 1
```
```