Java selection structure

Source: Internet
Author: User

Basic if selection structure: situations where a single or combined condition can be handled

if (condition) {--> result must be a Boolean value

Suggested omitting {} when there is only one statement in code block

}

IF-ELSE selection structure: Can handle simple conditional branching situation

if () {

Code Block 1

}else{

Code Block 2

}

Multiple if selection structure: A conditional branching condition that can handle continuous intervals

if (condition 1) {

Code Block 1

}else if (condition 2) {

Code Block 2

}else{

Code block 3

}

Nested IF selection structure: can handle complex conditional branching situations

if (condition 1) {

if (condition 2) {//code block 1

}else{//Code 2

}else{//Code 3

}

}

Switch selection structure: Multiple branches and condition judgment is the case of equivalence judgment

switch (expression)--integer or character {

case constant 1;  Statement Break

case constant 2;  Statement Break

......

default--> if no matching value is found

Statement

Break

}

Note: Break cannot be omitted, constants cannot be the same, expressions cannot be strings

The similarities and differences between the multiple if selection structure and the switch selection structure:

The same point: all structures used to handle multi-branch conditions

Different points:

Switch selection structure: can only handle the condition of the equivalent condition judgment, and the condition must be an integer or character type variable

Multi-Select structure: No switch-selection structure, especially when a variable is in a contiguous state

Java selection structure

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.