2018-03-07
1.IFIF (expression 1) {expression 2;} If the value of expression 1 is true, an expression of 2 is executed
Public class HelloWorld { publicstaticvoid main (string[] args) { Booleantrue; // Print Yes if it is set up if (b) { System.out.println ("yes"); }}}
2. Multiple expressions and an expression
Public classHelloWorld { Public Static voidMain (string[] args) {Booleanb =false; //If you have multiple expressions, you must include them with braces if(b) {System.out.println ("Yes1"); System.out.println ("Yes2"); System.out.println ("Yes3"); } //otherwise the expression 2 3 executes regardless of whether B is true if(b) System.out.println ("Yes1"); System.out.println ("Yes2"); System.out.println ("Yes3"); //If only one expression can be used without parentheses, it will look simple if(b) {System.out.println ("Yes1"); } if(b) System.out.println ("Yes1"); }}
3.if may encounter pits during use
In line 6th, the If is followed by a semicolon; And the semicolon is a complete expression.
If B is true, the semicolon is executed and the Yes is printed
If B is false, the semicolon is not executed and then print Yes
That way, it looks like it'll print yes anyway.
Public class HelloWorld { publicstaticvoid main (string[] args) { Booleanfalse; if (b); System.out.println ("yes");} }
4.if Else
else represents a case of non-establishment
Public class HelloWorld { publicstaticvoid main (string[] args) { Booleanfalse; if (b) System.out.println ("yes"); Else System.out.println ("no");} }
5.else if
else if is a multi-conditional judgment
Public classHelloWorld { Public Static voidMain (string[] args) {//if only using if, 4 judgments are performed inti = 2; 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 you use else if, once in 18 rows, the judgment is established, 20 rows and 22 lines of judgment will not be executed, saving the 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, and switch can use Byte,short,int,char,string,enum
Note: At the end of each expression, there should be a break;
Note: string is not supported before Java1.7, Java supports switch with string from 1.7, and compiles string into hash value, in fact, integer
Note: Enum is an enumeration type and is explained in detail in the enumeration section
Public classHelloWorld { Public Static voidMain (string[] args) {//If you use the IF Else intDay = 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"); ElseSystem.out.println ("What the hell is this?" "); //If you use the switch Switch(day) { Case1: System.out.println ("Monday"); Break; Case2: System.out.println ("Tuesday"); Break; Case3: System.out.println ("Wednesday"); Break; Case4: System.out.println ("Thursday"); Break; Case5: System.out.println ("Friday"); Break; Case6: System.out.println ("Saturday"); Break; Case7: System.out.println ("Sunday"); Break; default: System.out.println ("What the hell is this?" "); } }}
Java Basics/16th lesson: Control Process/Java if condition statement