4.Java Switch Statement-switch

Source: Internet
Author: User

When there are too many conditional judgment statements (if statements), you can use switch statements to write them. The basic structure of a switch statement is:
switch (integer) {
Case Integer value 1: statement; Break
Case Integer value 2: statement; Break
Case Integer value 3: statement; Break
..........................
Default: statement;
}

When the value of "integer" equals "integer value 1", "Integer value 2", "Integer value 3" ... , executes the corresponding statement, executes the completion exit Switch statement, if there is no equivalent value, executes the statement behind the default and executes the exit switch statement.

NOTE: The break statement is used to jump out of a switch statement.

Example: Output the current month.
public class control17{
public static void Main (string[] args) {
int i=8;
switch (i) {
Case 1:system.out.println ("January");
Case 2:system.out.println ("February");
Case 3:system.out.println ("March");
Case 4:system.out.println ("April");
Case 5:system.out.println ("May");
Case 6:system.out.println ("June");
Case 7:system.out.println ("July");
Case 8:system.out.println ("August");
Case 9:system.out.println ("September");
Case 10:system.out.println ("October");
Case 11:system.out.println ("November");
Case 12:system.out.println ("December");

Default:System.out.println ("Fault");
}
}
}

Operation Result:
Is August

4.Java Switch Statement-switch

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.