If, switch statements

Source: Internet
Author: User

Statement block

Block: A compound statement that refers to multiple Java statements enclosed by a pair of curly braces.

A statement block can contain another block of statements.

The statement block determines the scope of the variable.

A variable declared in a statement block can only be used in that statement block and within the inner statement block.

You cannot declare a variable with the same name in a nested two statement block.

If statement

The IF statement is one of the Java language selection control or branch control statements used to determine the given condition and determine the execution of one of the two actions given, based on the result of the decision (true or false).

Single Branch If statement: if (conditional expression) {statement block}

Dual-Branch If statement: if (conditional expression) {statement block 1}else{statement Block 2}

Multi-Branch If statement: if (conditional expression 1) {statement Block 1}

else if (conditional expression 2) {statement Block 2}

...

else if (conditional expression m-1) {statement block m-1}

else{statement block M}

If nesting:

if (conditional expression 1) {if (conditional expression 2) {statement Block 1}

else{statement block 2}}else{if (conditional expression 3) {statement block 3}

else{statement block 4}}

Switch statement

switch (arithmetic expression) {

case constant Expression 1: statement block 1

Break

Case constant Expression 2: statement block 2

Break

...

Defult statement block

Break

The data type of the "arithmetic expression" of parentheses after switch is a character type, a byte type, a short integer, an integer.

Executes the statement following this case when the value of the arithmetic expression is equal to the value of the constant expression following a case. If none of the statements following the case can be executed, then the statement following Defult is executed.

Each case constant expression must have a different value, or there will be conflicting behavior.

If, switch statements

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.