Switch case and break use in Java

Source: Internet
Author: User
Tags switch case
Switch can only compare numeric or character or class objects


First look at the parentheses of switch, and place the variable you want to take out the value. When the value is removed, the program begins to compare it to the number or character set in the case.
Executes the statement if it is met, and leaves the switch block until a break is encountered, and if there are no values or characters that match, the default statement is executed.
Default does not need to be, and if there are no actions to be handled by default, you can omit this part


Examples are as follows:
  @Testpublic void Switchcase () {this.net (2);} private void net (int network) {switch (network) {Case 1: {System.out.println ("BAIDU"); Case 2:case 3: {System.out.println ("Baidupeview"); Case 4: {break;} Case 5:case 6: {break;} Case 7:{break;} Case 8:case 9:{break;} Default: {System.out.println ("network [" + Network + "is not match ....); break;}}}
This example lists all the cases in switch case and sees that execution is all clear.

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.