Another one of those, “well, duh” moments… a very easy way to do wrap-around or circular array indexing.
1 | i = (i + 1) % N |
Where i is your current index and N is the length of the array.
Say you have an array of five elements. When you are currently on element of index 2, your next index will be:
1 | i = (2 + 1) % 5 = 3 |
However, once you get to the last element, index 4:
1 | i = (4 + 1) % 5 = 0 |
Viola, you are back at 0 again. I don’t know why but I really neglect the mod operator (%). It has some interesting uses.
Popularity: 1% [?]
