Java's Process Control statements

Source: Internet
Author: User
Tags case statement

One, Java conditional statements (If...else)

IfElse syntax: Multiple if syntax: nested IF syntax:

    if (condition 1) {                                               if (condition 1) {                              if (condition 1) {

        code block 1;                                                   code block 1;                                 if (condition 2) {

    }                                                               }                                                     code block 1;}

    else{                                                          else if (condition 2) {                          else{

         code block 2;                                                      code block 2;}                                    code block 2;}           

    }                                                                        else{                                else{

code block 3;} code block 3;}

Ii. switch of the Java conditional statement

Grammar:

Execution procedure: When the value of the expression after switch is the same as the value after the case statement, it starts down from that position until the break statement is encountered or the switch statement block ends, and the code of the default block is executed if there is no matching case statement.

Attention:

1, switch the value of the expression after the parentheses must be integer or character type

2. The value after the case can be a constant value , such as 1, 2, or a constant expression , such as a. T, but not a variable or an expression with a variable, such as a * 2

3, case matching, execute the program code in the matching block, if not met break will continue to execute the contents of the next block, until the break statement or switch statement block end.

4, the function of the same case statements can be combined, such as: Case1:

Case2:System.out.println ("We are very happy!") ");

Third, the Java Loop statement while

Syntax: while (loop condition) {

cyclic operation;}

Execution process: 1. Determine if the condition behind the while is established (True/false)

2. When the condition is established, execute the operation code within the loop, and then repeat steps b, 1, 2 until the loop condition is not established.

Features: first judgment, after execution

Iv. Do...while of the Java Loop statement

Syntax: do{

cyclic operation;}while (judging condition)

execution Process: 1. Perform a loop operation first and then determine if the loop condition is true

2. If the condition is true, proceed to step 1, 2 until the loop condition is not established.

Features: first execution, after judgment

Thus, the Do...while statement guarantees that the loop is executed at least once !

V. the Java Loop statement for

Syntax: for (loop variable initialization; loop condition; cyclic variable change) {

cyclic operation;}

Execution process:

1. Perform the loop variable initialization section, set the initial state of the loop, which executes only once in the entire loop

2. The loop condition is judged, the loop body code is executed if the condition is true, and if False, the loop is exited directly

3. Change the loop variable part, change the value of the loop variable, in order to make the next condition judgment

4. Re-execute step 2,3,4 in sequence until you exit the loop

Features: simpler and easier to read than the while and Do...while statement structure

VI. Break of the Java loop jump statement

In Java, we can use the break statement to exit the specified loop and execute the code immediately after the loop.

Example:

Running Result: 1 2 it's over.

Vii. continue of the Java loop jump statement

The function of continue is to skip the remaining statements in the loop body to perform the next loop.

For example, to print all the even numbers between 1--10, use the continue statement to implement the code:

Note: Break is the entire loop that contains it, and if it is a double loop and break is in the inner loop, you can only jump out of the inner loop and end the double loop in the outer loop.
Continue is the end of this loop to perform the next loop.

Java's Process Control statements

Related Article

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.