JAVA basics/Lesson 16th: Control Process/java if and if else, else if, and SWITCH condition statements, elseswitch
1. ifif (expression 1) {expression 2;} If expression 1 is true, execute expression 2.
Public class HelloWorld {public static void main (String [] args) {boolean B = true; // print if yes (B) {System. out. println ("yes ");}}}2. Multiple Expressions and one expression
Public class HelloWorld {public static void main (String [] args) {boolean B = false; // if there are multiple expressions, a large Arc must be used for inclusion. if (B) {System. out. println ("yes1"); System. out. println ("yes2"); System. out. println ("yes3");} // otherwise expression 2 3 executes if (B) System no matter whether B is true or not. out. println ("yes1"); System. out. println ("yes2"); System. out. println ("yes3"); // if there is only one expression, you do not need to write an arc. It looks a little simpler if (B) {System. out. println ("yes1");} if (B) System. out. println ("yes1 ");}}3. if problems may occur during use
In row 3, if has a semicolon, And the semicolon is also a complete expression.
If B is true, the semicolon is executed, and then yes is printed.
If B is false, do not execute this semicolon and print yes
In this way, it seems that yes will be printed anyway.
public class HelloWorld { public static void main(String[] args) { boolean b = false; if (b); System.out.println("yes"); }}4. if else
Else indicates not true
public class HelloWorld { public static void main(String[] args) { boolean b = false; if (b) System.out.println("yes"); else System.out.println("no"); }}5. else if
Else if is multi-condition judgment
Public class HelloWorld {public static void main (String [] args) {// if only if is used, int I = 2 will be judged four times; if (I = 1) system. out. println (1); if (I = 2) System. out. println (2); if (I = 3) System. out. println (3); if (I = 4) System. out. println (4); // if else if is used, once the judgment is true in 18 rows, the Judgment of 20 rows and 22 rows will not be executed, saves computing resources if (I = 1) System. out. println (1); else if (I = 2) System. out. println (2); else if (I = 3) System. out. println (3); else if (I = 4) System. out. println (4 );}}6. switch
The swich statement is equivalent to another expression of if else. switch can use byte, short, int, char, String, enum.
Note: Each expression ends with a break;
Note: String is not supported before Java 1.7. Java supports switch to use String from 1.7. After compilation, the String is converted to the hash value, which is actually an integer.
Note: enum is an enumeration type, which is described in detail in the enumeration section.
Public class HelloWorld {public static void main (String [] args) {// if you use if else int day = 5; if (day = 1) System. out. println ("Monday"); else if (day = 2) System. out. println ("Tuesday"); else if (day = 3) System. out. println ("Wednesday"); else if (day = 4) System. out. println ("Thursday"); else if (day = 5) System. out. println ("Friday"); else if (day = 6) System. out. println ("Saturday"); else if (day = 7) System. out. println ("Sunday"); else System. out. println ("what is this? "); // If you use switch (day) {case 1: System. out. println ("Monday"); break; case 2: System. out. println ("Tuesday"); break; case 3: System. out. println ("Wednesday"); break; case 4: System. out. println ("Thursday"); break; case 5: System. out. println ("Friday"); break; case 6: System. out. println ("Saturday"); break; case 7: System. out. println ("Sunday"); break; default: System. out. println ("what is this? ");}}}