Java Process Control Statements

Source: Internet
Author: User
Tags logical operators

The IF condition statement is implemented.

Have to say a few little secrets:

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

3 types of loops commonly used in Java: while, Do...while, for

Execution process:

<1>, perform the cycle operation first, and then determine if the loop condition is true

<2>, if the conditions are established, continue to implement < 1 >, < 2, until the cycle conditions are not established

Features: First execution, after judgment

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

Execution process:

<1>, executes the loop variable initialization section, sets the initial state of the loop, which executes only once in the entire loop

<2>, the judgment of the cyclic condition, if the condition is true, the loop body code is executed, and if False, the loop is exited directly

<3>, execute cyclic variable change part, change the value of the loop variable, in order to make the next condition judgment

<4>, re-execute < 2 >, < 3 >, < 4, until Exit loop

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

For example, output 1000 times "I adore the net", use for the implementation code is:

Some small details to keep in mind:

1. The three expressions in parentheses after the For keyword must be separated by ";", and three expressions can be omitted, but ";" cannot be omitted.

A. Omit "loop variable initialization", which can be initialized by an assignment statement before the for statement, such as:

B. Omitting "cyclic conditions" may cause the cycle to continue, which is what we often call a "dead loop" phenomenon, such as:

Avoid the appearance of a "dead loop" during programming, so for the above code you can use break to force out loops in the loop body (the usage of break is described later).

C. Omit "Cyclic variable change", you can change the loop variable in the loop body, such as:

2. For loop variable initialization and cyclic variable changes, you can use "," to initialize or change the values of multiple loop variables, such as:

In the code, the initialization variable part simultaneously assigns the initial value to two variables I and J, and the change part of the cyclic variable also changes the two variables, running the result:

3, the loop condition part can use the combination of logical operators expression, to express complex judgment conditions, but must pay attention to the priority of the operation, such as:

The code must satisfy both the variable i is less than 10, and I do not wait until 5 o'clock to loop, the value of the output variable i

In Java, we can use the break statement to exit a specified loop

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

Java Process Control 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.