The switch structure in Java

Source: Internet
Author: User
Tags case statement

The Chinese meaning of Switchkeyword is the meaning of switch and conversion, and the switch statement is especially suitable for making a set of variables equal inference in the conditional statement, which is much clearer than the if statement in structure.
The syntax format for the switch statement is:
switch (expression) {
Case value 1:
function code 1; 
[Break;]
Case Value 2:
function code 2; 
[Break;]
......
Default
function code 1; 
[Break;]
}
Syntax Description:
1. The type of expression can only be one of the 4 types of byte, short, char, and Int.
2, value 1, value 2 ... The value n can only be a constant or constant and cannot be a variable.
3, the function code part can write arbitrarily many sentences.
4, Breakkeyword means interrupt, refers to the end of the switch statement, the break statement is optional.
5, Case statement can have arbitrary multiple sentences, is the label statement.
6. The default statement can be written anywhere in the switch statement, functionally similar to the else in the IF statement.
Run Flow: When the value of an expression and the value after the corresponding Case statement are the same, run down from that location to the end of the switch statement, running, assuming that the break statement is encountered, ends the switch statement.
In the If-else if-else statement, the demo sample code, which gets every one months depending on the month, regardless of the leap year, is as follows:
int month = 10; 
int days = 0; 
Switch (month) {
Case 1:
days = 31; 
Break 
Case 2:
days = 28; 
Break 
Case 3:
days = 31; 
Break 
Case 4:
days = 30; 
Break 
Case 5:
days = 31; 
Break 
Case 6:
days = 30; 
Break 
Case 7:
days = 31; 
Break 
Case 8:
days = 31; 
Break 
Case 9:
days = 30; 
Break 
Case 10:
days = 31; 
Break 
Case 11:
days = 30; 
Break 
Case 12:
days = 31; 
Break 
}
System.out.println (days); 
Depending on the syntax of the switch statement, the code can also be simplified to include the following format:
int month = 10; 
int days = 0; 
Switch (month) {
Case 2:
days = 28; 
Break 
Case 4:
Case 6:
Case 9:
Case 11:
days = 30; 
Break 
Default
days = 31; 
}
System.out.println (days); 
Code Description: Because the switch statement is more equal each time, it is possible to combine the same case statements, and to merge other conditions into the default statement, which simplifies the writing of the scenario statement. The structure of the code is much simpler than the original code.
Although the syntax of switch can only be compared with the structure of the equivalent, in fact, some interval of the discriminant can be achieved by a certain transformation using the switch statement. For example, the If-else If-else statement demonstrates a sample of the fractional conversion in the example, then the interval of the fraction is between 0-100, assuming that one of the comparison, the number of case statements will be more, so can make a simple digital transformation, only a fraction of 10 digits and above the number, The interval for this number is reduced to 0-10, and the implementation code is the following:
int score = 87; 
Switch (SCORE/10) {
Case 10:
Case 9:
System.out.println (' A '); 
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 '); 
}
Of course, the switch statement is not very suitable for interval discriminant, and many other interval discriminant is usually implemented using the If-else if-else statement.
5.3.3 Summary
The IF statement can implement all the conditions in the program, the switch statement is particularly suitable for a series of points equal to the discriminant, the structure is relatively clear, and run faster than if statement to a little faster, in the actual code, in accordance with the need to use the corresponding statement to implement the logic function required by the program.

The switch structure in Java

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.