Java Junior Development Programmer (Unit VI)

Source: Internet
Author: User

1.FOR Loop structure:

The loop structure is divided into four parts.

> Initial part: Sets the initial state of the loop, such as setting the variable I to record the number of cycles is 0.

> Loop body: Repeated execution of the code, that is, the output "study hard, day up!" ”。

> Iteration section: The part to be executed before the next loop starts, in the while loop structure as part of the loop body, such as using "i++;" To accumulate the number of cycles.

> Cycle conditions: Determine whether to continue the cycle of conditions, such as the use of "i<100" to determine whether the number of cycles has reached 100.

2. In the For loop structure, these parts are also necessary, otherwise the loop will be wrong. The general format for the For loop is as follows:

Grammar:

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

Loop body

}

Expression 1: The initial part of the loop structure, assigning the initial value to the loop variable.

Expression 2: Loop condition of the looping structure.

Expression 3: The iteration portion of a looping structure, typically used to modify the value of a loop variable.

Meaning of 3 expressions in 3.FOR loop structure

An expression Form Function Lifting columns
Expression 1 Assignment statements The initial part of the loop structure, assigning the initial value to the loop variable int i=0
Expression 2 Conditional statements Cyclic conditions for cyclic structures i<100
Expression 3 Assignment statements, usually using the i++ or--operator The iterative part of a looping structure, usually used to modify the value of a loop variable i++

4.for Execution Process:

①②④

for (initial part; condition judgment; change loop variable value) {

③ cycle operation; The loop body is executed

}

The 5.FOR loop structure is executed in the following order:

(1) Execute the initial part (int i=0;).

(2) The judgment of the cyclic condition (i<100).

(3) Judging results based on cyclic conditions

> If True, the loop body is executed.

> If False, exits the loop, neither step (4), step (5) is executed.

(4) Execute the Iteration section and change the value of the loop variable (i++).

(5) Repeat step (2) ~ Step (4) in turn until you exit the for loop structure.

Visible, in the for loop structure, the initial part of expression 1 is executed only once.

6. Jump statement:

The >break: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 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.

The >continue:continue statement can be used for a for loop structure or for a while and do-while loop structure. In the For loop structure, the continue statement causes the program to jump to the iteration section before judging the loop condition. If true, the next loop is resumed, otherwise the loop is terminated. In the while loop structure, when the continue statement is executed, the program will directly judge the loop condition. A continue sentence can only be used in a loop structure.

7. Different execution order

*while Cycle Structure: The condition is judged first, then the loop body is executed. If the condition is not true, exit the loop.

*do-while Loop structure: First executes the loop body, then carries on the condition judgment, the circulation body executes at least once.

*FOR Loop structure: First executes the initialization part, then carries on the condition judgment, then executes the loop body, finally carries on the iteration part computation. If the condition is not set, jump out of the loop.

Different application conditions:

In case of problem solving, the for loop structure is usually chosen for the uncertain cycle times. For cases with uncertain cycle times, the while and do-while loop structures are usually chosen.

This chapter summarizes:

In a loop, you can use the break and continue statements to control the process

The *break statement is used to terminate a loop, and the program jumps to the next statement outside the loop body

The *continue statement is used to jump out of this loop and into the next loop statement.

Java Junior Development Programmer (Unit VI)

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.