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;
                }
        }
}

No comments:

Post a Comment

Translate