Chapter 4 cycle structure, Chapter 4 Cycle Structure

Source: Internet
Author: User

Chapter 4 cycle structure, Chapter 4 Cycle Structure

1. Features of the loop structure:

1) not endless

2) It will continue only when certain conditions are met. It is called a "cyclic condition"

3) Exit the loop structure when the condition is not met

4) The loop structure is a series of identical or similar operations that are called "loop operations".

2. The loop structure in java must meet the following three conditions:

1) initialize cyclic variables, such as: int I = 1;

2) A conditional expression is required to determine whether the cyclic body ends, such as: I <= 100;

3) There must be a statement that changes the expression value of the judgment condition, such as: I ++;

The three steps can be summarized into three words: "first", "sentence", "change".

3. while loop syntax

While (loop condition ){

Cyclic Operation

}

Feature: Judge first and execute.

4. do-while loop syntax

Do {

Cyclic Operation

} While (Cyclic condition );

Feature: perform the command at least once.

Note: do-while ends.

5. Differences between while and do-while

1) syntax is different: the while loop structure is judged first and then executed; the do-while loop structure is executed first and then determined. When the initial condition does not meet the loop conditions, the while loop will not be executed once, the do-while loop is executed once no matter what the situation is.

2) the do-while loop structure is followed by a semicolon, while does not.

6. for Loop syntax

For (1. parameter initialization; 2 conditions; 4. Parameter superposition ){

Cyclic Operation

}

Features: 3. Judge before execution.

7. the syntax of the for loop contains four parts.

1) initialize the cyclic variable

2) condition expression used to determine whether the loop body ends

3) Statement for changing the expression value of the judgment Condition

4) loop body

Difference: The for loop contains all three conditions in brackets;

For loops are generally used to know the number of loops, while is used to determine the number of loops.

8. Jump statement

Break: it is used to change the control flow of a program. It is mostly used in a loop and combined with a condition structure and a branch structure.

Purpose: jump out of the entire Loop

Continue: it can only be used in a loop. The remaining statements in the loop language exit to execute the next loop.

Function: ends the current loop and enters the next loop.

9. Difference Between break and continue

The break statement ends the entire loop structure. The end loop of the continue statement must be determined based on the conditions. The End Of The continue statement is the statement after the loop is kicked, not the end loop structure.

10. return Statement

Two functions: one is to return the value of the specified type of method, and the other is to end the execution of the method (that is, to return to the call of the method, only the previous return statement must be written, ).

11. Steps for using Loops

1) determine the cycle conditions and body

2) apply the syntax

3) check whether the system can exit normally.

12. Procedure

1) Guess the error location and set the breakpoint

2) start debugging and run in one step

3) Observe Variables

4) discover problems

5) Correct the code and restart

6) Solve the Problem

 

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.