Created
March 27, 2015 21:17
-
-
Save ecornell/1ed747db34b9400facfa to your computer and use it in GitHub Desktop.
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
/** | |
* Created by ecornell on 3/27/2015. | |
*/ | |
public class ForLoops { | |
public static void main(String[] args) { | |
// While - (loop possibly zero times) | |
int i=1; | |
while (i <= 3) { // condition is checked before inner block is ran | |
System.out.println("while -> " + i); | |
i++; | |
} | |
// For - Simulate a While Loop (loop possibly zero times) | |
for (int j = 1; j <= 3; j++) { // condition is checked before inner block is ran | |
System.out.println("for (while) -> " + j); | |
} | |
// --------- | |
// Do/While (loop at least one time) | |
int k = 5; | |
do { | |
System.out.println( "do/while -> This will print once" ); | |
k++; | |
} while (k <= 3); // condition is checked after inner block is ran | |
// While - Simulate a Do/While Loop (loop at least one time) | |
boolean ranAtLeastOnce = false; | |
int l = 5; | |
while (ranAtLeastOnce == false || l <= 3) { | |
System.out.println( "while (do/while) -> This will print once" ); | |
ranAtLeastOnce = true; | |
} | |
// For - Simulate a Do/While Loop (loop at least one time) | |
ranAtLeastOnce = false; | |
for (int m = 5 ; ranAtLeastOnce == false || m <= 3; m++) { | |
System.out.println( "for (do/while) -> This will print once" ); | |
ranAtLeastOnce = true; | |
} | |
} | |
} | |
// Output | |
// | |
// while -> 1 | |
// while -> 2 | |
// while -> 3 | |
// | |
// for (while) -> 1 | |
// for (while) -> 2 | |
// for (while) -> 3 | |
// | |
// do/while -> This will print once | |
// | |
// while (do/while) -> This will print once | |
// | |
// for (do/while) -> This will print once |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment