Last active
November 1, 2017 08:24
-
-
Save rosuH/a3ce3ff0a8e65122b86e689b0ba15f5c to your computer and use it in GitHub Desktop.
Use the modulo operator to implement loops
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
/* | |
* 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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
a % b = a
IFa < b
. So we can implement a loop by calling the method.