The best Java branch structure - if ... else / switch Tutorial In 2024, In this tutorial you can learn if statement,if ... else statement,if ... else if ... else statement,Nested if ... else statement,switch statement,
Sequence structure only the order of execution can not be judgments and choices, and therefore require a branched structure.
Java has two branch structure:
If a statement contains a boolean expression and one or more statements.
If the statement with the following syntax:
if(布尔表达式) { //如果布尔表达式为true将执行的语句 }
If the Boolean expression evaluates to true, the if statement block of code. Otherwise If statement block behind the code execution.
public class Test { public static void main(String args[]){ int x = 10; if( x < 20 ){ System.out.print("这是 if 语句"); } } }
The above code is compiled results are as follows:
这是 if 语句
Later if statement with else statements when the boolean expression of the if statement is false, else statement block is executed.
if /en. else used 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 is compiled results are as follows:
这是 else 语句
Later if statement with elseif /en. else statement, this statement can detect a variety of possible scenarios.
Use if, else if, else statement, the need for attention to the following points:
if /en. else syntax 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("This is else statement"); } } }
The above code is compiled results are as follows:
Value of X is 30
Nested if-else statement is legitimate. This means that you can use if or elseif statement in another if or elseif statement.
Nested if /en. else syntax is as follows:
if(布尔表达式 1){ ////如果布尔表达式 1的值为true执行代码 if(布尔表达式 2){ ////如果布尔表达式 2的值为true执行代码 } }
You can be like the if statement nested else if /en. 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 is compiled results are as follows:
X = 30 and Y = 10
switch statement to determine a value of a variable with a range of values are equal, and each value is called a branch.
switch syntax is as follows:
switch(expression){ case value : //语句 break; //可选 case value : //语句 break; //可选 //你可以有任意数量的case语句 default : //可选 //语句 }
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("Excellent!"); break; case 'B' : case 'C' : System.out.println("Well done"); break; case 'D' : System.out.println("You passed"); case 'F' : System.out.println("Better try again"); break; default : System.out.println("Invalid grade"); } System.out.println("Your grade is " + grade); } }
The above code is compiled results are as follows:
Well done Your grade is C