I’m setting up a system that is going to cycle through items in an array indefinitely.
I’m wondering if there is an efficient or simple way to do this.
So the basic set up is like this: I have an array, let’s say with five values.
I want to go through those values in order, array[0], array[1], … array[4] and then back to array[0]. (Or possibly in reverse order.
Off the top of my head, the best method I can think of to do this to check a value and set up an if statement.
value++;
if (value >= array.length) {
value = 0;
}
It works, but I’m wondering if there is a simpler method built in. (Plus I would need a second method if I wanted one to go in reverse order.) Kind of like, if I were working with a byte, it would automatically revert back to zero when I increment it past 255. Is there a method where I can automatically roll over a value in conjunction with the length of an array?