Introduction to Java Basics (iv) Loop structure

Source: Internet
Author: User


One, for loop

Format: for (loop variable initial value setting; loop condition Boolean value; statement executed after each loop) {

Circulation body;

}

Execution process: Initial value setting is calculated first.

The Boolean value is then computed, and if true, the loop body is executed once, and then the statement executed after each loop is executed.

The Boolean value is then computed, and if true, the loop body is executed once, and then the statement executed after each loop is executed.

The Boolean value is then computed, and if true, the loop body is executed once, and then the statement executed after each loop is executed.

......

The Boolean value is then computed, and if false, it stops.

Second, while Loops

Statement format: while (Boolean expression) {

statement block;

}

Execution order: Determine the value of the Boolean expression first, if true. The statement block is executed.

The value of the Boolean expression is then judged, if true. The statement block is executed.

The value of the Boolean expression is then judged, if true. The statement block is executed.

......

It ends when the Boolean expression is false.

Third, Do-while Loops

Format: do{

statement block;

}while (boolean-expression);

Execution order: Executes the statement block and then determines the Boolean expression, if True.

Continue execution of the statement block, and then determine the Boolean expression, if True.

Continue execution of the statement block, and then determine the Boolean expression, if True.

Continue execution of the statement block, and then determine the Boolean expression, if False. Stop it.

Four, Break with the Continue

The use of break and continue in the loop.

Break: Stop Looping

Continue: Stop this cycle

V. Summary

Comparison of three types of cycles

1. For the same problem, three kinds of cycles can be substituted for each other.

2, for loop function is stronger than while,do...while, but if the initial value of the loop variable is not obvious, then you can use while or do...while to enhance the structure and readability of the program.

3, * * * Write loop must prevent infinite cycle (dead cycle).

Introduction to Java Basics (iv) Loop structure

Related Article

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.