Java Branch structure


In the program, the branch structure makes a judgment and decides which branch to enter.

Sequential structures can only be executed sequentially, can not be judged and selected, so the branch structure is required. Branch structure has the following, although the usage is different, but the essence is interlinked.

if...else

The most basic form is if-else

public class Exercise {
   public static void main (String args []) {
     // branch starts
     if (2> 3) // judge the condition
         System.out.println ("is true"); / / if true is the implementation of the statement behind the if
     else
         System.out.println ("is false"); / / if the leave after the implementation of else else behind the statement
     // branch ends

     System.out.println ("End"); // After the branch is complete, continue
   }
}

You can not else else, only if; else do not perform any operation:

public class Exercise {
    public static void main (String args []) {
        // branch starts
        if (2> 3) // judge the condition
            System.out.println ("is true"); / / if true is the implementation of the statement behind the if
        // branch ends

        System.out.println ("End"); // After the branch is complete, continue
    }
}

Can also be nested, branch and then branch:

public class Exercise {
    public static void main (String args []) {
        int num = 9;
        if (num> = 0) // judge positive and negative
            if (num% 2 == 0) // judgment parity
                System.out.println (num + "is positive even or 0");
            else
                System.out.println (num + "is the odd number");
        else
            if (num% 2 == 0) // judgment parity
                System.out.println (num + "is negative even");
            else
                System.out.println (num + "is negative odd");
    }
}

A more common form of nesting is if ... else if ... else if ... else else:

public class Exercise {
    public static void main (String args []) {
        int A = 1, B = 2, C = 3, D = 4;
        if (D < A)
            System.out.println ("D is not the maximum");
        else if (D < B)
                System.out.println ("D is not the maximum");
            else if (D < C)
                    System.out.println ("D is not the maximum");
                else
                    System.out.println ("D is the maximum");
    }
}

switch

The switch statement is like a multidirectional switch, and any path can be selected according to the criteria.

The condition of the switch must be byte, int, short or char.

Common structure is as follows:

public class Exercise {
    public static void main (String args []) {
        char favour = 'r';
        switch (favorite) {
        case 'r': System.out.println ("I like red"); break;
        case 'b': System.out.println ("I like blue"); break;
        case 'g': System.out.println ("I like green"); break;
        case 'y': System.out.println ("I like yellow"); break;
        case 'w': System.out.println ("I like white"); break;
        default: System.out.println ("I do not like"); break;
        }
    }
}

The result is

I like the color red

Please note that "break", if there is no break at the end, then the program will be from the line that meet the conditions to continue the implementation of all the next line. In other words, case is a start tag, and the stop flag is break. The contents of the first eligible case and the next break will be executed.

if there is no break:

public class Exercise {
    public static void main (String args []) {
        char favour = 'y';
        switch (favorite) {
        case 'r': System.out.println ("I like red");
        case 'b': System.out.println ("I like blue");
        case 'g': System.out.println ("I like green");
        case 'y': System.out.println ("I like yellow");
        case 'w': System.out.println ("I like white");
        default: System.out.println ("I do not like");
        }
    }
}

The result is

I like yellow
I like white
I do not like it anymore

if there are no eligible cases, execute default.

In fact, anyone can use the switch to achieve the function, with if-else can be achieved. But in some cases, the use of switch will be more concise and clear.




Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook