Java05-java Basic Grammar (iv) cyclic structure

Source: Internet
Author: User

Java05-java Basic Grammar (iv) cyclic structure

Loop structure (repetition/iteration): Repeating partial statements based on conditions

1. While loop structure

while (conditional expression) {

loop body statement;

}

1) Syntax: A, while is a keyword

B, "conditional expression" must be enclosed in parentheses.

C, "{}" means the control range of the loop, if there is no "{}", while the control range to the first semicolon thereafter;

2) Execution Process:

(1) operation "conditional expression", if the result is true, transfer to (2); If the result is false, transfer to (3)

(2) Execute once, loop body statement, transfer (1)

(3) Exit cycle

2. Do-The While loop:

do{

loop body statement;

}while (conditional expression);

1) Syntax:

A, do is a keyword and must be used in conjunction with while and cannot be used alone

The semicolon after the B, while (conditional expression) cannot be omitted, indicating the end of the loop

2) Execution Process:

(1) Execute "loop body statement" first

(2) Run "conditional expression", if true, transfer to (1); If False, transfer to (3)

(3) Exit cycle

Emphasis: Two types of cycle differences:

1) The While loop is first judged to be true and then executes the loop body statement; The loop body statement is executed at least once, regardless of whether the condition is true or do-while.

2) While loop "conditional expression" does not have a semicolon after the do-while loop "conditional expression" must have a loop.

3. For loop:

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

loop body statement;

}

1) Syntax:

(1) for is a keyword

(2) Three expressions can be omitted partially or completely, but semicolons cannot be omitted

Expression 1: Assigning an initial value to a variable

Expression 2: Conditional expression, controlling whether the loop continues

Expression 3: The statement that the loop tends to end

2) Execution Process:

(1) First operation "Expression 1"

(2) operation "Expression 2", if True, into (3), if False, transfer (4)

(3) Execute "loop body statement", operation "Expression 3", transfer to (2)

(4) Exit cycle

4, for Loop, while loop, do-while cycle between transitions

(1) For loop

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

loop body statement;

}

(2) While loop

Expression 1;

while (expression 2) {

loop body statement;

Expression 3;

}

(3) Do-while cycle

Expression 1;

do{

loop body statement;

Expression 3;

}while (expression 2);

Java05-java Basic Grammar (iv) cyclic structure

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.