Switch-Case Analysis

Source: Internet
Author: User
Tags case statement
The following analysis is not necessarily useful in practice. However, in order to learn, especially for exams, I think it is necessary to understand it. Because of some things, you may have formed a "code set ", haha (usually the mindset ). It is not about studying grammar, but about how to understand things flexibly.

Default usually appears in the last line of the switch-case statement. See the following code:

  1. Public class testswitch {
  2. Public static void main (string [] ARGs ){
  3. Byte A = 1;
  4. Switch (){
  5. Case 1: system. Out. println ("A"); break;
  6. Case 2: system. Out. println ("B"); break;
  7. Case 3: system. Out. println ("C"); break;
  8. Case 4: system. Out. println ("D"); break;
  9. Default: system. Out. println ("default ");
  10. }
  11. }
  12. }

The output result can be imagined:,

But let's make a transformation and remove the break in the above case (this should be avoided in practice, it is wrong, so we do this to study the syntax ), you may also think of the output:
  1. Public class testswitch {
  2. Public static void main (string [] ARGs ){
  3. Byte A = 1;
  4. Switch (){
  5. Case 1: system. Out. println ("");
  6. Case 2: system. Out. println ("B ");
  7. Case 3: system. Out. println ("C ");
  8. Case 4: system. Out. println ("D ");
  9. Default: system. Out. println ("default ");
  10. }
  11. }
  12. }

Output result:

A B c d default ==================================================== I want can default appear anywhere in the switch? The answer is yes. One Kind of habit is that default is put at the end. This is a habit and a good habit for us to discuss and learn, so I put it somewhere in the middle to see what will happen: First of all, I should set the value in the code to 100, so that the switch cannot find a match, it will execute the default statement. If there is no break at this time, it will exit after executing the default statement or execute the following case statement block, in our imagination, we may think that default should be the last code segment to be executed, but this is not the case:
  1. Public class testswitch {
  2. Public static void main (string [] ARGs ){
  3. Bytes A = 100;
  4. Switch (){
  5. Case 1: system. Out. println ("");
  6. Case 2: system. Out. println ("B ");
  7. Default: system. Out. println ("default ");
  8. Case 3: system. Out. println ("C ");
  9. Case 4: system. Out. println ("D ");
  10. }
  11. }
  12. }

Running result: Default c d ======================== I will not analyze it.


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.