The switch can only compare values or characters, but do not think that it is not useful if so. If appropriate, it is more efficient than if Else. The syntax structure of the switch is as follows:
Switch (variable name or expression ){
Case must contain numbers or characters:
Statement 1;
break;
Case must contain numbers or characters:
Statement 2;
break;
default:
Statement 3;
}
First, let's look at the brackets of the switch, where the variable you want to take out the value. After a value is taken out, the program starts to compare it with the number or character set in case. If it matches the value, execute the statement until the switch block is left after the break is encountered; if no matching value or character exists, the statement after default is executed, which is not required by default. If no default action is needed, this part can be omitted.
Next, let's take a look at how to use switch to rewrite the score level comparison of example 3.26.
?
Example 3.27 scorelevel2.java
Import java. util. collections;
Public class scorelevel2 {
Public static void main (string [] ARGs ){
Required bytes = new bytes (system. In );
System. Out. Print ("Enter the score :");
Int score = bytes. nextint ();
Int level = (INT) score/10;
Switch (level ){
Case 10:
Case 9:
System. Out. println ("get ");
Break;
Case 8:
System. Out. println ("B ");
Break;
Case 7:
System. Out. println (" C ");
Break;
Case 6:
System. Out. println ("D ");
Break;
Default:
System. Out. println ("e (fail )");
}
}
}
In this program, division is used to obtain the operator number after calculation. If the number is greater than 90, the operator number divided by 10 must be 9 or 10 (100 points of time ). In case
10 does not contain any statement or break, so it will continue to be executed until the break leaves the switch. Therefore, if the student score is 100, the score level of A is also displayed. If the comparison condition is not 10 ~ 6. These values will execute the default statement, which indicates that the number of businesses is less than 6, so the score level of the students is displayed as E.
Note that the equal signs after case are colons rather than semicolons, which are often incorrect characters. If the comparison is a character, remember to add a single quotation mark (''). For example:
case 'A':
What is the difference between this program and the program that uses if to determine the score level? If it is purely a number or character, we recommend that you use switch because it will only
Switch the brackets to retrieve the variable value once, and then compare the value with the following case. However, if you use if, You must retrieve the variable value every time you encounter a conditional condition, the difference in efficiency is here. For example:
if(a == 1)
// ...
else if(a == 2)
// ...
else if(a == 3)
// ...
In the worst case, when a = 3, this program fragment requires three comparisons, and the value of variable A must be retrieved once for each comparison. Switch:
switch(a) {
case 1:
// ...
break;
case 2:
// ...
break;
case 3:
// ...
break;
}
In this program fragment, only the value of variable A is taken out in the brackets of the switch at the beginning, and then the following case is compared one by one. The difference in efficiency is here. Of course, it is not good to use if. In case of composite conditions, the switch cannot help, because it cannot combine complex conditional statements in the switch, then you have to use if. Simply put, if and switch can be used flexibly together.