Created
December 3, 2017 16:37
-
-
Save ShmuelMofrad/fa7ccfa31340e3e0a353e35c47fe7e7d to your computer and use it in GitHub Desktop.
While(true) loops is not bad forever.
This file contains hidden or 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
public class WhileLoopsTrueForever { | |
public static void main(String[] args) { | |
firstPhase(); | |
sum(5, 2017); | |
secondPhase(); | |
iamOK(); | |
} | |
private static void firstPhase() { | |
int counter = 0; | |
/* it is always true and it's not always good! */ | |
while (true) { | |
counter++; | |
System.out.println("counter: " + counter); | |
if (counter == 5) { | |
break; | |
} | |
} | |
} | |
private static void secondPhase() { | |
boolean runWhile = true; | |
int counter = 0; | |
/* it is not always true! */ | |
while (runWhile) { | |
counter++; | |
System.out.println("counter-2: " + counter); | |
if (counter == 5) { | |
runWhile = false; | |
} | |
} | |
} | |
private static void sum(int a, int b) { | |
System.out.println("sum A and B is: " + Integer.sum(a, b)); | |
} | |
private static void iamOK() { | |
System.out.println("I'm OK!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment