Switch-case of the destroyed person. Note the following:

Source: Internet
Author: User
The following is a simple method of Switch:

Switch (){

Case B;} the value in Section A must be of the int type, or can be automatically converted to an int type expression. That is to say, Part A can be of the byte/short/Char/INT type. It should be emphasized that the value of Part B in the program must be a single byte/short/Char/INT type value or a final type variable. However, final variables also have requirements, that is, they must be constants during compilation. For more information, see the following program section: Final int A = 0; Final int B; the second statement is a variable that cannot be recognized during compilation because it is not initialized. Of course, this statement is also incorrect. Therefore, the value after case can be a constant value or a final value.
Let's look at the following section:
Public class testswitch {public static void main (string [] ARGs) {byte A = 11; Switch (a) {// cCase 11: system. out. println ("11"); break; case225: system. out. println ("11"); break; // d }}}
Is the code correct? The answer is no. Although it is valid in C, that is, the value of the byte type can appear in the switch, but the statement at D, that is, the value after the second case, is 225, which exceeds the byte range, so it is incorrect. The value after case cannot be repeated. Therefore, pay attention to it.
= Write Break; statement. So what are the consequences? The following small program section will tell you: public class testswitchcase {public static void main (string [] ARGs) {byte A = 2; Switch () {Case 1: system. out. println ("A"); Case 2: system. out. println ("B"); Case 3: system. out. println ("C"); Case 4: system. out. println ("D"); default: system. out. println ("default ");
}}}========= The output result is: B c d default ---------------------------- have you seen it? Even the default command is executed. Pay attention to the end character break; then OK.
It seems that the syntax is simple, but the simpler it is, the easier it is to ignore it. Once an error occurs in a large project, it is hard to find out what is wrong, and then ....... haha. The following is a simple method of Switch:

Switch (){

Case B;} the value in Section A must be of the int type, or can be automatically converted to an int type expression. That is to say, Part A can be of the byte/short/Char/INT type. It should be emphasized that the value of Part B in the program must be a single byte/short/Char/INT type value or a final type variable. However, final variables also have requirements, that is, they must be constants during compilation. For more information, see the following program section: Final int A = 0; Final int B; the second statement is a variable that cannot be recognized during compilation because it is not initialized. Of course, this statement is also incorrect. Therefore, the value after case can be a constant value or a final value.
Let's look at the following section:
Public class testswitch {public static void main (string [] ARGs) {byte A = 11; Switch (a) {// cCase 11: system. out. println ("11"); break; case225: system. out. println ("11"); break; // d }}}
Is the code correct? The answer is no. Although it is valid in C, that is, the value of the byte type can appear in the switch, but the statement at D, that is, the value after the second case, is 225, which exceeds the byte range, so it is incorrect. The value after case cannot be repeated. Therefore, pay attention to it.
= Write Break; statement. So what are the consequences? The following small program section will tell you: public class testswitchcase {public static void main (string [] ARGs) {byte A = 2; Switch () {Case 1: system. out. println ("A"); Case 2: system. out. println ("B"); Case 3: system. out. println ("C"); Case 4: system. out. println ("D"); default: system. out. println ("default ");
}}}========= The output result is: B c d default ---------------------------- have you seen it? Even the default command is executed. Pay attention to the end character break; then OK.
It seems that the syntax is simple, but the simpler it is, the easier it is to ignore it. Once an error occurs in a large project, it is hard to find out what is wrong, and then ....... haha.

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.