May 10, 2021 Java
1. Java Branch Structure - if... else/switch
4. if... e lse if... Else statement
Sequential structures can only be executed sequentially and cannot be judged and selected, so branch structures are required.
Java has two branch structures:
An if statement contains a Boolean expression and one or more statements.
The syntax used in the If statement is as follows:
if(布尔表达式)
{
//如果布尔表达式为true将执行的语句
}
If the value of the Boolean expression is true, the block of code in the if statement is executed. O therwise, the code behind 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 above code compiles and runs as follows:
这是 if 语句
The if statement can be followed by the else statement, and when the boolean expression value of the if statement is false, the else statement block is executed.
if... The use of 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 above code compiles and runs as follows:
这是 else 语句
If statement can be followed by else if... else statement, which can detect a variety of possible scenarios.
When using if, else if, else statements, you need to pay attention to the following points:
if... T he else syntax format 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 above code compiles and runs as follows:
Value of X is 30
It is legal to use nested if-else statements. T his means that you can use an if or else if statement in another if or else if statement.
Nested if... The else syntax format is as follows:
if(布尔表达式 1){
////如果布尔表达式 1的值为true执行代码
if(布尔表达式 2){
////如果布尔表达式 2的值为true执行代码
}
}
You can nest else if like an if statement... else。
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 above code compiles and runs 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 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 above code compiles and runs as follows:
良好
你的等级是 C