Showing posts with label then. Show all posts
Showing posts with label then. Show all posts

Sunday, 14 June 2015

Flow Control 2: Switch Blocks

public class SwitchBlockDemo {
        public static void main(String[] args) {
                int A = 0;

                // if-then-else purely for comparison to switch
                if(A == 0){
                        System.out.println("A = " + A + "\n");
                        ++A;
                } else if(A == 1) {
                        System.out.println("A = " + A + "\n");
                        ++A;
                } else if(A == 2) {
                        System.out.println("A = " + A + "\n");
                        ++A;
                } else if(A == 3) {
                        System.out.println("A = " + A + "\n");
                        ++A;
                } else if(A == 4) {
                        System.out.println("A = " + A + "\n");
                        ++A;
                } else if(A == 5) {
                        System.out.println("A = " + A + "\n");
                        ++A;
                } else {
                        System.out.println("A = " + A);
                        System.out.println("A is not 0. A will be reset.");
                        System.out.println("If only we had some way to loop this code.");
                        A = 0;
                }

                /* Unlike the if-then-else code above, the switch
                 * block below will execute all the code that comes
                 * after the matching case statement.
                 */
                switch (A) {
                        case 0: System.out.println("A = " + (A++));
                        case 1: System.out.println("A = " + (A++));
                        case 2: System.out.println("A = " + (A++));
                        case 3: System.out.println("A = " + (A++));
                        case 4: System.out.println("A = " + (A++));
                        case 5: System.out.println("A = " + A);
                        default:
                        System.out.println("A is not 0. A will be reset.\n");
                                A = 0;
                                break;
                }

                // This is a condensed version of the above switch block.
                switch (A) {
                        case 1: case 2: case 3: case 4: case 5:
                                System.out.println("\nA = " + (A++));
                        default:
                                System.out.println("A is not equal to 0. A will be reset");
                                System.out.println("If only I could loop this code.");
                                A = 0;
                                break;
                }

                /* Adding break statements "breaks" the program out of the switch
                 * block. The default code executes when none of the case statements
                 * are satisfied. A break statement is not needed here. But it can
                 * help make the code more robust and readable.
                 */
                switch (A) {
                        case 0:
                                System.out.println("A = " + (A++));
                                break;
                        case 1:
                                System.out.println("A = " + (A++));
                                break;
                        case 2:
                                System.out.println("A = " + (A++));
                                break;
                        case 3:
                                System.out.println("A = " + (A++));
                                break;
                        case 4:
                                System.out.println("A = " + (A++));
                                break;
                        case 5:
                                System.out.println("A = " + A);
                                break;
                        default:
                                System.out.println("A is not 0. A will be reset.");
                                A = 0;
                                break;
                }
        }
}

Note:
When coding the switch blocks in the examples above. Eclipse decided they should not be indented. Forcing Eclipse to indent these switch blocks properly wasn't too hard. It just meant correcting the error a few times.

Indentation is clearly not mandatory in Java. However Oracle's own examples are indented. So I presume this is how switch blocks are supposed to be written.

Saturday, 13 June 2015

Flow Control 1: if-then-else

public class IfThenElse {
        /* Programs normally execute one line after the other
         * from top to bottom. But sometimes we need to make
         * decisions while a program is running. Some of these
         * decisions will send a program down one path or another.
         *
         * To do this in Java we use if-then and if-then-else
         * statements.
         */
        public static void main(String[] args) {
                int A = 0;

                /* Basic if-then statement
                 * Since there is only one line of code to be
                 * executed after the if-then statement we
                 * don't need braces {}.
                 *
                 * NOTE: We don't actually need to type "then", it is implied.
                 */
                if(A == 0)
                        System.out.println("First run.\nA = " + A);

                /* A more complex if-then statement. We now need braces
                 * to enclose our "block" of code.
                 */
                if(A == 0) {
                        System.out.println("\nSecond Run.");
                        System.out.println("A = " + (A++));
                        System.out.println("A = " + (A++));
                        System.out.println("A = " + (A++));
                        System.out.println("A = " + (A++));
                        System.out.println("A = " + (A++));
                        System.out.println("A = " + A);
                }

                /* Both of the examples above will only print A if it
                 * is 0 when tested. When A is not 0 we need to be
                 * able to take a different course of action. Even
                 * if that is simply telling the user what has happened.
                 * We do this by adding else to if-then.
                 */
                if(A == 0) {
                        System.out.println("\nThird Run.");
                        System.out.println("A = " + (A++));
                        System.out.println("A = " + (A++));
                        System.out.println("A = " + (A++));
                        System.out.println("A = " + (A++));
                        System.out.println("A = " + (A++));
                        System.out.println("A = " + A);
                } else {
                        System.out.println("\nThird Run.");
                        System.out.println("A is not 0. A will be reset.");
                        A = 0;
                }

                /* It is also possible to add additional if statements.
                 * This allows us to have many options. In the code below
                 * only of the options will execute depending on the value
                 * of A.
                 */
                if(A == 0){
                        System.out.println("\nFourth Run.");
                        System.out.println("A = " + A);
                        ++A;
                } else if(A == 1) {
                        System.out.println("\nFourth Run.");
                        System.out.println("A = " + A);
                        ++A;
                } else if(A == 2) {
                        System.out.println("\nFourth Run.");
                        System.out.println("A = " + A);
                        ++A;
                } else if(A == 3) {
                        System.out.println("\nFourth Run.");
                        System.out.println("A = " + A);
                        ++A;
                } else if(A == 4) {
                        System.out.println("\nFourth Run.");
                        System.out.println("A = " + A);
                        ++A;
                } else if(A == 5) {
                        System.out.println("\nFourth Run.");
                        System.out.println("A = " + A);
                        ++A;
                } else {
                        System.out.println("\nFourth Run.");
                        System.out.println("A = " + A);
                        System.out.println("A is not 0. A will be reset.");
                        System.out.println("If only we had some way to loop this code.");
                        A = 0;
                }
        }
}

Translate