A branch structure
Conditional statement: if ... else
if statement:
An if statement contains a Boolean expression and one or more statements, if the boolean expression evaluates to true, the if statement in the code block is executed, otherwise the code block following the if statement.
grammar:
IF (Boolean expressions) { // statement if the Boolean expression is true to execute }
if ... else statement:
if behind the statement, when the Boolean expression of the if statement is false, else statement block is executed with the else statement.
grammar:
IF (Boolean expressions) { // If the boolean expression evaluates to true } the else { // If the boolean expression evaluates to false }
if behind the statement with else if ... else statement, this statement can be detected by a variety of possible scenarios.
grammar:
IF (Boolean expression 1) { Code block is executed; if the execution code is true // Boolean expression 1 } the else IF (Boolean expression 2) { Code block is executed; if the execution code is true // Boolean expression 2 } ... ... else{ Execute code block; // execute code if not more than the Boolean expression is true }
When using if, else if, else statement, you need to pay attention to the following points:
1.if statement at most after all else if statement 1 else statement, else statement.
2.if statement can have several else if statements, they must be before the else statement.
3. Once wherein a detection else if statement is true, else if other else statement are skipped and execution.
Example:
public class Test { public static void main(String args[]){ int x = 30; if( x == 10 ){ System.out.print("Value of X is 10"); }else if( x == 20 ){ System.out.print("Value of X is 20"); }else if( x == 30 ){ System.out.print("Value of X is 30"); }else{ System.out.print ( "This is the else statement" ); } } }
The above code is compiled results are as follows:
Value of X is 30
Second, the branch structure 2
switch case statement
grammar:
Switch (expression The) { case value: // statements BREAK ; // optional case value: // statements BREAK ; // optional // you can have any number of case statement default : // optional // statements }
Rules:
1.switch (expression) of the expression of the return value must be one of the following types: byte, Short, char, int, enumeration, String;
2.case clause values must be constant , and all values in the case clause should be different;
3.default clause is optional , when no matching Case, performs default
4.break statement is used after executing programs out of a case that the switch statement branch block; if not break, the program will execute the order to the end switch
When 5.switch case execution, we will first match, the match is successful return to the current value of the case, and then depending on whether break, to determine whether to continue the output, or out of the judge.
Example 1:
public class Test1 { public static void main(String args[]){ String season = "winter"; switch(season){ case"spring": System.out.println("spring is a little girl"); break; case"summer": System.out.println("summer is a young boy"); break; case"autumn": System.out.println("autumn is a big man"); break; case"winter": System.out.println("winter is a old people"); break; default: System.out.println("who am i?"); break; } } }
Operation results: winter is a old people
switch and if statements comparison:
1. The main use an if statement, using a broader range of if statements
2. If it is judged not more specific values, and in accordance byte, short, int, char four types recommended switch; (IF statements can also be used, but is more efficient switch statement)
3. The determination of the segment, the result is a boolean type judgment shall be used if statement
Error Summary: In example 1 is a compilation encounter the following error:
After Baidu that error because the switch statement to determine the conditions acceptable int, byte, char, short, can not accept other types only JDK version 1.7 can support more than String
Solution : Right-click the project Test1 ----> select properties ----> select Java Compiler ----> select version 1.7 or more