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.

No comments:

Post a Comment

Translate