Process Control statements for the Java Foundation

Source: Internet
Author: User

1, sequential statements

The code executes sequentially from the top down.

In fact, the sequential structure of the code we write is ubiquitous. Typically, the code is executed from the top down, left to right, in addition to the following structural statements that alter the order of execution.

2, conditional statement

Divided into three categories,

Single If statement: if (conditional expression) {

Executes the statement when the conditional expression is established;

}

If-else statement: if (conditional expression) {

Executes the statement when the conditional expression is established;

}else{

The conditional expression does not immediately execute the statement;

}

If-else-if statement: if (conditional expression 1) {

Conditional expression 10% executes the statement immediately;

}else if (conditional expression 2) {

Conditional expression 20% executes the statement immediately;

}else{

When the above conditional expression is not valid, execute the statement;

}

Conditional statement Nesting: for example:

if (conditional expression 1) {

if (conditional expression 2) {

Statement 1;

}else{

Statement 2;

}

}else{

Statement 3;

}

The execution principle of the nested form is the same as in the previous one, but it is possible to make a judgment after judging it once.

3, select statement

Format: switch (expression) {

Case "Result of expression 1":

Statement 1;

Break

Case "result of expression 2":

Statement 2;

Break

Default

Statement 3;

Break

}

Issues to be aware of when using switch: The value type of the result of an expression in a switch post-parenthesis: char short int long String (JDK1.7 only) enum type

The result of an expression after a case must be a determined value, not a variable.

Switch statements and if statements can be converted to each other,

Switch is recommended when the type is fixed and the received data is constant

It is recommended to use the IF statement when you receive a range or need to compare

4, Loop statement

When type loop: while (loop condition) {

Circulation body;

}

Until the type cycle: do{

Circulation body;

}while (cyclic condition)

The difference between the two: in the case of the same expression, until the type loop is more than the loop to execute the cycle body.

For loop: for (initialization statement; loop condition; Iteration statement) {

Circulation body;

}

5, extended

Ternary operators

Format: An expression? The expression is set to execute the statement: The expression does not form the execution of the statement;

The IF () {}else{} statement can be converted to and from the ternary operator.

Process Control statements for the Java Foundation

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.