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:
- Public class testswitch {
- Public static void main (string [] ARGs ){
- Byte A = 1;
- Switch (){
- Case 1: system. Out. println ("A"); break;
- Case 2: system. Out. println ("B"); break;
- Case 3: system. Out. println ("C"); break;
- Case 4: system. Out. println ("D"); break;
- Default: system. Out. println ("default ");
- }
- }
- }
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:
- Public class testswitch {
- Public static void main (string [] ARGs ){
- Byte A = 1;
- Switch (){
- Case 1: system. Out. println ("");
- Case 2: system. Out. println ("B ");
- Case 3: system. Out. println ("C ");
- Case 4: system. Out. println ("D ");
- Default: system. Out. println ("default ");
- }
- }
- }
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:
- Public class testswitch {
- Public static void main (string [] ARGs ){
- Bytes A = 100;
- Switch (){
- Case 1: system. Out. println ("");
- Case 2: system. Out. println ("B ");
- Default: system. Out. println ("default ");
- Case 3: system. Out. println ("C ");
- Case 4: system. Out. println ("D ");
- }
- }
- }
Running result: Default c d ======================== I will not analyze it.