/** Program: EssentialFlow Purpose: simple demonstration of Java control flow @author john@december.com @version 1.05; 21 Nov 95 Note: updated to 1.00 beta Changes: Quick version of if-then branching won't compile in beta Output: BRANCHING OK. This is remaining OK. Human body. LOOPING Hi up: 1 Hi up: 2 Hi down: 3 Hi down: 2 Hi down: 1 Hi again: -50 Hi again: 0 Hi again: 50 Still OK: 0 Still OK: 1 Still OK: 2 OK = true counter = 500 */ class EssentialFlow{ public static void main (String args[]) { System.out.println("BRANCHING"); // If-then branching. boolean OK = true; if (OK) System.out.println("OK."); else System.out.println("NOT OK."); // Quick version of if-then branching. // won't compile under beta // OK ? System.out.println("Quick OK.") : // System.out.println("Quick NOT OK."); // Mutiple line if-then branches. if (OK) { System.out.println("This is"); System.out.println("remaining OK."); } else { System.out.println("Or is it"); System.out.println("NOT OK?"); } // SWITCH type branching int temperature = 37; switch (temperature){ case (0): System.out.println("Freezing water!"); break; case (37): System.out.println("Human body."); break; case (100): System.out.println("Boiling water!"); break; default: System.out.println("Some temperature."); } System.out.println("LOOPING"); // Simple FOR loop up. int counter; for (counter = 1; counter < 3; counter++){ System.out.println("Hi up: " + counter); }; // Simple FOR loop down. for (counter = 3; counter > 0; counter--){ System.out.println("Hi down: " + counter); }; // FOR loop with non ++ step size expression. for (counter = -50; counter < 99; counter = counter + 50){ System.out.println("Hi again: " + counter); } // While loop with condition checked at the start counter = 0; OK = true; while (OK) { System.out.println("Still OK: " + counter); counter++; OK = (counter < 3); } // do-while loop executes at least once; condition check at end counter = 500; OK = true; do { System.out.println("OK = " + OK + " counter = " + counter); counter++; OK = (counter < 3); } while(OK); } }