1. Select the structure if statement format and its use
A: if statement format:
if(Compare Expression 1) {
Statement body 1;
}else if(compare expression 2) {
Statement body 2;
}else if(compare expression 3) {
Statement body 3;
}
...
else {
Statement body n+1;
}
B: Execution process:
First, calculate the comparison expression 1 to see if its return value is true or false.
If true, execute statement body 1 and end if statement.
If it is false, then calculate the comparison expression 2 to see if its return value is true or false.
If true, execute statement body 2 and end if statement.
If it is false, then calculate the comparison expression 3 to see if its return value is true or false.
If all are false, the statement body n+1 will be executed.
C: Note: The last else can be omitted, but it is recommended not to omit it. You can prompt error values outside the range.
eg:
import java.util.Scanner;class Demo_If { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //Keyboard input, while(true) { System.out.println("Please enter score"); //Prompt input int a = sc.nextInt(); //Keyboard input is int type if (a>100|a<0) { //Dead loop, convenient for testing System.out.println("You entered the score incorrectly"); }else if (a>=90&a<=100) { System.out.println("A, etc."); }else if (a>=80&a<90) { System.out.println("B, etc."); }else if (a>=70&a<80) { System.out.println("C, etc."); }else if (a>=60&a<70) { System.out.println("D, etc."); }else if (a<60) { System.out.println("E, etc."); }else { System.out.println("You entered the wrong grade"); } }}} 2. Select the structure switch statement format and its use
A: switch format:
switch(expression) {
case value 1:
Statement body 1;
break;
case value 2:
Statement body 2;
break;
…
default:
Statement body n+1;
break;
}
B: format explanation of switch statement
(Basic data types, as long as they can be promoted to int, refer to enumerations in the data types (JDK1.5) and String (JDK1.7))
C: Execution process
Calculate the value of the expression first
Then match the following case. If there is a corresponding statement, otherwise execute the default controlled statement.
eg
import java.util.*;class Dome_If3{ public static void main(String[] args) { //System.out.println("Hello World!"); Scanner sc =new Scanner(System.in); //Keyboard entry while(true) { //Dead loop facilitates testing System.out.println("Please enter the number of weeks to be converted"); //Keyboard entry prompt in week = sc.nextInt(); switch(week) { case 1: System.out.println("1 of the week"); break; case 2: System.out.println("Week 2"); break; case 3: System.out.println("Week 3"); break; case 4: System.out.println("Week 4"); break; case 5: System.out.println("Week 5"); break; case 6: System.out.println("Week 6"); break; case 7: System.out.println("Sunday"); break; default: System.out.println("You entered the number incorrectly, please re-enter"); } } }}3: Summarize the respective usage scenarios of switch statements and if statements
Switch is recommended to use when judging fixed values
If it is recommended to use it when judging the interval or range
*What you can do with switch, if you can do it, but if you can do it, it won't work if you do it with a SLR.
The above article briefly talks about the difference between selecting structured if statements and switch statements is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.