Last active
December 10, 2015 07:48
-
-
Save headquarters/4403355 to your computer and use it in GitHub Desktop.
A reminder to myself of how to handle array indexing in a "round robin" fashion, using the modulus operator.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//assume variables currentIndex, nextIndex, arrayLength exist | |
//moving "forward", positive incrementing | |
nextIndex = (currentIndex + 1) % arrayLength; | |
//moving "backward", negative incrementing | |
if(currentIndex == 0){ | |
nextIndex = arrayLength - 1; | |
} else { | |
nextIndex = (currentIndex - 1) % arrayLength; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment