S1/using Java to understand program logic/04-selection Structure (ii)

Source: Internet
Author: User

the code block after each case can have multiple statements that can have a set of statements and do not need to be enclosed in "{}" .

Note: in JDK1.6 and previous versions, theswitch selection structure must have an integer variable or a character variable, but in JDK1.7, the condition of the switch selection structure can also be used The String type.

Select a structure summary:

Java provides two of the most important selection structures, they are the if selection structure and the switch selection structure.

(i) the if selection structure in Java, including the following form.

1. Basic If selection structure

if ( condition ) {

code block

}

2.if-else selection structure

if ( condition ) {

code block 1

}else{

code block 2

}

3. Multiple If selection structure

if ( condition 1) {

code block 1

}else if ( condition 2) {

code block 2

}else{

code block 3

}

4. Nested If selection structure

if ( condition 1) {

if ( condition 2) {

code block 1

}else{

code block 2

}

}else{

if ( condition 3) {

code block 3

}else{

code block 4

}

}

(ii) switch selection structure in Java

The Scanner object's Hasnextint () method is called to determine whether the characters entered by the user from the keyboard are legitimate.

S1/using Java to understand program logic/04-selection Structure (ii)

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.