Java Loop Structure Second speaking

Source: Internet
Author: User

First, what is a for loop structure

The main function of a looping statement is to execute a piece of code repeatedly until a certain condition is met. The loop structure can be divided into four parts:

1. Initial part: Set the initial state of the loop.

2. Loop body: Code that executes repeatedly.

3. Iteration part: The part to be executed before the next loop starts, as part of the loop body in the while loop structure.

4. Cycle conditions: Determine whether to continue the cycle of conditions.

In the For loop structure, these parts are also necessary, otherwise the loop will have an error.

Ii. syntax for A For loop

for (expression 1; expression 2; expression 3) {

Loop body

}

The for here is the keyword for this loop structure. Where expression 1 is an assignment statement, Expression 2 is a conditional statement, Expression 3 is also an assignment statement, usually using the + + or--operator.

Third, the execution order of the For loop structure

1. Perform the initial section.

2. Determine the cyclic conditions.

3. Judge the result according to the cyclic condition. If true, the loop body is executed and, if False, exits the loop, and the following steps do not loop.

4. Perform the iteration section to change the value of the loop variable.

5. Repeat the successive steps.

Iv. Frequently Asked Questions

1. Omit expression 1. In actual programming, if this occurs, you need to assign a value to the loop variable before the for statement.

2. Omit expression 2. In this case, the cyclic condition is not judged, and the loop will not terminate, and a dead loop occurs. Modification method: 1. Add Expression 2. 2. Add break in the loop body to force the bounce out of the loop.

3. Omit expression 3. After omitting, the value of the loop variable is not changed, and a dead loop occurs. The value of the loop variable can be changed in the loop body.

4. All three expressions are omitted. The statement in this case is syntactically correct, but is logically wrong, refer to the above three methods for modification.

V. Jump statements

Java supports three types of jumps: Break;continue;return.

The break statement terminates a loop and causes the program to jump to the next statement outside the loop body. The statement after break in the loop body is no longer executed, and the loop stops executing. The break statement can be used not only in the for loop structure, but also in the while and do-while loop structures. The break statement is typically used with an if conditional statement.

Java Loop Structure Second speaking

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.