Java programming things 34 -- switch statement syntax

Source: Internet
Author: User
Tags case statement

Java programming those things 34 -- switch statement syntax Zhengzhou game college Chen yuefeng from: 5.3.2 switch statementThe Chinese meaning of the switch keyword is the meaning of switch and conversion. The switch statement is especially suitable for determining the equality of a group of variables in the Condition Statement, and is much clearer in structure than the if statement. The syntax format of 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 expression type can only be byte, short, Char, or Int. 2. Value 1, value 2... Value n can only be a constant or constant, not a variable. 3. You can write any number of sentences in the function code section. 4. The break keyword indicates that the switch statement is terminated. The break statement is optional. 5. The case statement can have any number of statements, which are label statements. 6. The default statement can be written at any position in the switch statement, and is similar to the else in the IF statement. Execution Process: when the expression value is the same as the value after the corresponding case statement, it is executed from this position to the end of the switch statement, if a break statement is encountered, the switch statement is executed. In the IF-else if-else statement, the number of days of each month is obtained based on the month, regardless of the leap year. The sample code 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); according to the switch statement syntax, the code can also be simplified to 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 compares equal relations each time, you can combine case statements with the same functions, in addition, other conditions can be merged into the default statement, which simplifies the writing of the case statement. The structure of the Code is much simpler than the original code. Although the switch syntax can only compare equal structures, in fact, some intervals can be identified through certain transformations using the switch statement. For example, if-else statement example shows the score conversion example, the score range is between 0 and. If the number of case statements is more than one by one, therefore, you can perform a simple numeric transformation by comparing only the ten or more digits of the score, so that the number range is reduced to 0-10. The implementation code is as follows: 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 discrimination. More interval discrimination is normal. Or use the IF-else if-else statement. 5.3.3 SummaryThe IF statement can implement all the conditions in the program. The switch statement is particularly suitable for the identification of a series of equal points, with a clear structure and a faster execution speed than the if statement, in actual code, you can use the corresponding statements as needed to implement the logic functions required by the program.

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: 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.