Showing posts with label for. Show all posts
Showing posts with label for. Show all posts

Saturday, 4 July 2015

Flow Control 8: Branching Statements - return

public class ReturnDemo {
        /* The return statement is the simplest of the
         * branching statements. It is used to  exit
         * the current method and return a value.
         */
        public static String month1 (int value) {
                String a = "Welcome to January!";
                return a;
        }
        public static String month2 (int value) {
                String a = "Welcome to February!";
                return a;
        }
        public static String month3 (int value) {
                String a = "Welcome to March!";
                return a;
        }
        public static String month4 (int value) {
                String a = "Welcome to April!";
                return a;
        }

        public static void main(String[] args) {
                String text = "If I wasn't so lazey the year would be complete.";

                for (int month = 1; month <= 12; month++) {
                        switch (month) {
                                case 1:
                                        System.out.println(month1(month));
                                        break;
                                case 2:
                                        System.out.println(month2(month));
                                        break;
                                case 3:
                                        System.out.println(month3(month));
                                        break;
                                case 4:
                                        System.out.println(month4(month));
                                        break;
                                default:
                                        System.out.println(text);
                                        break;
                        }
                }
        }
}

Monday, 15 June 2015

Flow Control 5: Branching Statements - break

public class BreakDemo {
        /* break statements are used to terminate
         * switch blocks, for, while and do-while loops.
         */
        public static void main(String[] args) {
                // Variable declarations;
                int i = 2;
                int searchFor = 3;
                int[] numbers = {0,1,2,3,4,5};
                boolean foundIt = false;

                // Simple switch block with break statements.
                switch (i) {
                        case 0:
                                System.out.println("A = " + (i++));
                                break;
                        case 1:
                                System.out.println("A = " + (i++));
                                break;
                        case 2:
                                System.out.println("A = " + (i++));
                                break;
                        default:
                                System.out.println("Resetting A.");
                                i = 0;
                                break;
                }

                // Simple for loop with a break statement.
                for (i = 0; i < numbers.length; i++) {
                        if (numbers[i] == searchFor) {
                                foundIt = true;
                                break;
                        }
                }
                if (foundIt) {
                        System.out.println(searchFor + " is at index " + i);
                } else {
                        System.out.println(searchFor + " could not be found.");
                }

                // Simple while loop with a break statement.
                while(foundIt) {
                        System.out.println("i = " + (i++));
                        if (i == 100) {
                                System.out.println("i = " + i);
                                i = 0;
                                break;
                        }
                }

                // Simple do-while loop with a break statement.
                do {
                        if (i == 100) {
                                System.out.println("i = " + i);
                                break;
                        }
                        System.out.println("i = " + (i++));
                } while (foundIt);

                /* Since there is no code included in either
                 * of the while or do-while loops to make foundIt
                 * false. Both loops should effectively be infinite.
                 * However using if-then with break gives us another
                 * opportunity to test for a condition that should
                 * terminate the loop.
                 */
        }
}

Sunday, 14 June 2015

Flow Control 4: for

public class ForLoopDemo {
        /* In addition to the while and do-while loops.
         * The Java language also provides the for loop.
         * This is used to iterate through a range of values.
         */
        public static void main(String[] args) {
                // Count to 5 with a basic for loop.
                for (int A = 0; A <= 5; A++){
                        System.out.println("A = " + A);
                }

                /* So the code above executes for as long as the
                 * value of A is less than or equal to 5.
                 *
                 * The next example will work it's way through
                 * an array. This is called an enhanced for loop.
                 */
                int[] B = {6,7,8,9,10};
               
                for (int item : B) {
                        System.out.println("B = " + item);
                }

                // We can also do the same for strings.
                String[] months = {"January","February","March","April","May","June",
                        "July","August","September","October","November","December"};

                for (String item : months) {
                        System.out.println("The month is " + item);
                }
        }
}

Translate