Skip to content

Instantly share code, notes, and snippets.

@rosuH
Last active November 1, 2017 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rosuH/a3ce3ff0a8e65122b86e689b0ba15f5c to your computer and use it in GitHub Desktop.
Save rosuH/a3ce3ff0a8e65122b86e689b0ba15f5c to your computer and use it in GitHub Desktop.
Use the modulo operator to implement loops
/*
* use modulo operator to implement loops
*/
public class Loop {
// index
private static int index;
// a objects array
private static obj[] mObjs = new obj[]{
new obj(0),
new obj(1),
new obj(2),
new obj(3),
new obj(4),
};
// call this method
private static void fun() {
index = index % mObjs.length();
// do something here
do();
}
public static void main(String[] agrs) {
while(1) {
fun();
}
}
}
@rosuH
Copy link
Author

rosuH commented Oct 12, 2017

a % b = a IF a < b. So we can implement a loop by calling the method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment