Switch case and break in java

Source: Internet
Author: User
Tags switch case

Switch case and break in java
Switch can only compare values, characters, or class objects


First, let's look at the brackets of the switch, where the variable you want to take out the value. After a value is taken out, the program starts to compare it with the number or character set in case,
If yes, execute the statement until the switch block is left after the break is met. If no matching value or character exists, the default statement is executed,
Default is not required. If there is no action to be processed by default, you can save this part.


Example:

@ Testpublic void switchCase () {this.net (2);} private void net (int network) {switch (network) {case 1: {System. out. println ("BAIDU"); break;} case 2: case 3: {System. out. println ("BAIDUpeview"); break;} 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 situations of the switch case. It is clear that one execution is complete.

Related Article

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.