Java Syntax Basics-Program Flow Control-Select structure-switch statements

Source: Internet
Author: User

switch ( expression ) The data type of the value of the selected expression can only be a byte short int char

{

case takes value 1:

execute the statement;

break;

case takes value 2:

execute the statement;

break;

..... ...

Default:

execute the statement;

break;}


Switch statement features:

There are only four types of 1.switch statement choices:byte,short,int , Char .

2. alternative answers are not in order. But execution must be performed from the first case .

3. defaultis executed only if all of the case does not match .

4. End Feature: Encounters a break, or executes to the end of the curly brace.

Description

in theSwitch (An expression), the expression inside the Swith can only be aan integer expressionorEnumeration Constants(larger font), an integer expression can be an int primitive type or an integer wrapper type, because,Byte,short,char can be implicitly converted to int, so, these types as well as these types ofPackage Typeis also possible. Obviously, long and string types do not conform to the syntax of switch, and cannot be implicitly converted to int types, so they cannot be used in Swtich statements.

        int x = 3;         switch (x)         {             default:                 system.out.println ("no");                 break;             case 1:                 system.out.println ("C");                 break;            case  4:                 System.out.println ("a");  &Nbsp;              break;             case 2:                 system.out.println ("B");                 break;} Break on the last side can be omitted.


Switch statements, and if very much like, in development, in the end who use it?

When deciding on a specific number of values, you can use ifor switch, which is recommended for use with switch.

If you can also judge the interval, you can also judge the result of the operation as a Boolean type.

So if the application is more.




Java Syntax Basics-Program Flow Control-Select structure-switch statements

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.