Java Basic---Switch for Java conditional statements (21)

Source: Internet
Author: User
Tags case statement

Switch of the Java conditional statement

Using a switch statement is more concise when you need to make an equivalent judgment on an option. For example: According to the rank of the exam, give the top 4 different prizes. First prize notebook one, second prize IPAD 21, Third prize mobile power one; the last one to reward u disk.

Syntax: 650) this.width=650; "src=" http://img.mukewang.com/5364f4c800010b1f01620289.jpg "/>

Execution procedure: When the value of the expression after switch is the same as the value after the case statement, it starts down from that position until the break statement is encountered or the switch statement block ends, and the code of the default block is executed if there is no matching case statement.

such as: 650) this.width=650; "src=" http://img.mukewang.com/5364f5e300010b6004790310.jpg "/>

Have to say a few little secrets:

1, switch the value of the expression after the parentheses must be integer or character type

2. The value after the case can be a constant value , such as 1, 2, or a constant expression , such as a. T, but not a variable or an expression with a variable, such as a * 2

3, case matching, execute the program code in the matching block, if not met break will continue to execute the contents of the next block of case until the break statement or switch statement block end such as 650) this.width=650; "src = "Http://img.mukewang.com/5364f6440001425d04810245.jpg"/>, running result: 650) this.width=650; "src="/HTTP/ Img.mukewang.com/5364f74800013d9101330090.jpg "/>

4. You can combine case statements with the same functionality, such as 650) this.width=650; "src=" http://img.mukewang.com/5364f6950001a57604280064.jpg "/>

5, the default block can appear in any position , you can also omit


Code:

public class Helloworld {
public static void Main (string[] args) {
Char today= ' Day ';
Switch (today) {
Case ' one ':
Case ' three ':
Case ' V ':
System.out.println ("Eat steamed buns");
Break
Case ' II ':
Case ' four ':
Case ' VI ':
System.out.println ("eat fritters");
Break
Default
System.out.println ("Eat Chairman's Package");

}
}
}

Running result: Eat Chairman's Package

This article is from "Ghost" blog, please make sure to keep this source http://caizi.blog.51cto.com/5234706/1547730

Java Basic---Switch for Java conditional statements (21)

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.