The sequential structure can only be executed sequentially and cannot make judgments and selections, so a branch structure is needed.
Java has two branching structures:
An if statement contains a Boolean expression and one or more statements.
The syntax of the If statement is as follows:
if(布尔表达式) { //如果布尔表达式为true将执行的语句}
If the Boolean expression evaluates to true, the code block in the if statement is executed. Otherwise, the code following the If statement block is executed.
public class Test { public static void main(String args[]){ int x = 10; if( x < 20 ){ System.out.print("这是if 语句"); } } }
The results of compiling and running the above code are as follows:
这是if 语句
The if statement can be followed by an else statement. When the Boolean expression value of the if statement is false, the else statement block will be executed.
The usage of if…else is as follows:
if(布尔表达式){ //如果布尔表达式的值为true }else{ //如果布尔表达式的值为false }
public class Test { public static void main(String args[]){ int x = 30; if( x < 20 ){ System.out.print("这是if 语句"); }else{ System.out.print("这是else 语句"); } } }
The results of compiling and running the above code are as follows:
这是else 语句
The if statement can be followed by an else if...else statement, which can detect a variety of possible situations.
When using if, else if, else statements, you need to pay attention to the following points:
The syntax format of if...else is as follows:
if(布尔表达式1){ //如果布尔表达式1的值为true执行代码}else if(布尔表达式2){ //如果布尔表达式2的值为true执行代码}else if(布尔表达式3){ //如果布尔表达式3的值为true执行代码}else { //如果以上布尔表达式都不为true执行代码}
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("这是else 语句"); } } }
The results of compiling and running the above code are as follows:
Value of X is 30
It is legal to use nested if-else statements. This means you can use an if or else if statement within another if or else if statement.
The nested if…else syntax format is as follows:
if(布尔表达式1){ ////如果布尔表达式1的值为true执行代码if(布尔表达式2){ ////如果布尔表达式2的值为true执行代码} }
You can nest else if...else just like if statements.
public class Test { public static void main(String args[]){ int x = 30; int y = 10; if( x == 30 ){ if( y == 10 ){ System.out.print("X = 30 and Y = 10"); } } } }
The results of compiling and running the above code are as follows:
X = 30 and Y = 10
The switch statement determines whether a variable is equal to a value in a series of values. Each value is called a branch.
The switch syntax format is as follows:
switch(expression){ case value : //语句break; //可选case value : //语句break; //可选//你可以有任意数量的case语句default : //可选//语句}
The switch statement has the following rules:
public class Test { public static void main(String args[]){ //char grade = args[0].charAt(0); char grade = 'C'; switch(grade) { case 'A' : System.out.println("优秀"); break; case 'B' : case 'C' : System.out.println("良好"); break; case 'D' : System.out.println("及格"); case 'F' : System.out.println("你需要继续努力"); break; default : System.out.println("无效等级"); } System.out.println("你的等级是" + grade); } }
The results of compiling and running the above code are as follows:
良好你的等级是C