[Arrangement] switch default

Source: Internet
Author: User

Let's take a look at the following sample code:

package flowcontrol;   
1.
2. public class SwitchCase {
3. // first default
4. public static void testFirst(int i) {
5. switch (i) {
6. default:
7. System.out.println("default");// first default
8. case 1:
9. System.out.println("one");
10. case 2:
11. System.out.println("two");
12. case 3:
13. System.out.println("there");
14. }
15. }
16.
17. // last default
18. public static void testLast(int i) {
19. switch (i) {
20. case 1:
21. System.out.println("one");
22. case 2:
23. System.out.println("two");
24. case 3:
25. System.out.println("there");
26. default:
27. System.out.println("default");// last default
28. }
29. }
30.
31. // middle default
32. public static void testMiddle(int i) {
33. switch (i) {
34. case 1:
35. System.out.println("one");
36. case 2:
37. System.out.println("two");
38. default:
39. System.out.println("default");// middle default
40. case 3:
41. System.out.println("there");
42.
43. }
44. }
45.
46. public static void main(String[] args) {
47. // first default
48. testFirst(2);
49. System.out.println("------------------");
50. testFirst(9);
51.
52. System.out.println("|||||||||||||||||||||||||||||||||||");
53.
54. // last default
55. testLast(2);
56. System.out.println("----------------");
57. testLast(9);
58.
59. System.out.println("|||||||||||||||||||||||||||||||||||");
60. // middle default
61. testMiddle(2);
62. System.out.println("----------------");
63. testMiddle(9);
64.
65. }
66.
67. }

The output is as follows:

1.    two   
2. there
3. ------------------
4. default
5. one
6. two
7. there
8. |||||||||||||||||||||||||||||||||||
9. two
10. there
11. default
12. ----------------
13. default
14. |||||||||||||||||||||||||||||||||||
15. two
16. default
17. there
18. ----------------
19. default
20. there

The analysis is as follows:

(1) the key point of the switch statement is the entry point. If the entry point does not have a break, it will be executed to the end, or it will encounter a break.
(2) If no matching value exists, default is the entry point. after default is entered, it will be the same as the normal entry point. If the following statement is not executed

No picture, no truth, and no hands-on code analysis are just a few minutes away ~

Or that sentence: simple and reliable ~

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.