Switch case usage in. Java

Source: Internet
Author: User
Tags switch case

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.