Java Learning Switch selection structure

Source: Internet
Author: User

Learn to program the Java Learning switch selection structure released, welcome to visit through Xuebiancheng8.com

The previous analysis of Java's Process control is divided into the choice structure, the loop structure, wherein the choice structure can be divided into the if choice structure and the switch structure. Let's analyze the switch...case structure.

The switch...case structure is also used to implement the selection structure

The syntax is as follows:

Switch (variable) {

CASE constant 1:

......

Break

Case constant 2:

......

Break

Default

......

Break

}

Syntax is too abstract, next look at the code

int a = 10;

Switch (a) {

Case 5:

System.out.println ("A has a value of 5");

Break

Case 10:

System.out.println ("A has a value of 10");

Break

Default

System.out.println (the value of "a" is neither 5 nor 10 ");

Break

}

In the above example, to determine the value of a, when the value of a is 5 o'clock, execute the code of Case 5, when the value of a is 10 o'clock, the code of the execution case 10, when a is not 5 is not 10, the code that executes the default

What is the difference between if and switch...case, if used with the condition is range, say a>10, this representation range. And Switch...case is used when the condition is fixed value.

Of course, there are a lot of things to be aware of switch...case, such as when necessary, then the case must be added after the Break;switch parentheses can be Byte,short,int,string,char, enumeration of these types, Nothing else can be put in brackets.

Please come and visit your classmates xuebiancheng8.com.

Http://xuebiancheng8.com/play/goodgoodstudy_54_daydayup.html


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.