_java of IF and switch statements of Java control statements

Source: Internet
Author: User

Java If statement

The Java control statements are divided into three categories: ① sequence structure, ② selection structure, ③ cyclic structure.

--------------------------------------------------------------------------------

The selection structure is divided into: ① single selection structure, ② double choice structure, ③ multiple choice structure.

Mainly involved: if_else, switch, while, break and continue, for.

If single selection structure

Test the conditional expression once, and if the test is true, execute the following statement or skip the statement.

Example (use of the math class)

Random (), returns the decimal number of a positive symbol, and the interval size is [0,1).

Note the problem with the scope of if: If statement is not added {}, its control scope is limited to the first sentence. (General suggestion Plus {}, develop a good habit)

If-else Dual Selection structure

When the conditional expression is true, execute the statement block 1, otherwise, execute the statement block 2, which is the else part.

If-else If-else Multi-selection structure

Let me introduce you to the Java switch statement

Java Control Statement--switch statement

The equivalent judgment of the above if statement can be replaced by a switch.


Note that after each case, you typically add a break to indicate that the current case is finished; prevent the case from penetrating, that is, continue to execute the case until it encounters a break.

The following example in turn exploits the case penetration phenomenon.

Example

JDK7.0 new features: Enhanced switch

Before JDK7, the switch (expression) {...}, where the expression result can only be int (the Byte, short, char) that can be automatically converted to int, enumerated types.

But in JDK7, the result of an expression can also be a string.

(PS: Automatic type conversion: A data type with small capacity can be automatically converted to a large capacity data type.) Byte (1 bytes) →short (2 bytes) →int (4 bytes). )

The above is a small set to share the Java control statements If, switch statements, I hope to help.

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.