While loop statement, Do-while Loop statement, for Loop statement text tutorial detailed

Source: Internet
Author: User

the format of awhile loop statement:

1. Format:

while (condition of the loop) {    loop statement;}

2, while the Loop statement to note matters:
1) The While Loop statement is typically the number of times a variable is controlled by its loop.
2) The Loop body code of the WHILE loop statement if there is only one statement, you can omit the curly brace. However, it is not recommended to omit the same as if judgment statement.
3) While the judgment condition of the loop statement can not be followed by a semicolon, otherwise it will affect the effect of execution.

Let's leave two fun programs for everyone:
1, the realization of guessing the number of games, if you do not guess the right can continue to enter the number you guessed, if guessed to stop the program.

2, guessing the game upgrade version, can only guess at most three times, if the last opportunity left to remind users.

Second, do While loop statement (use less, while more close to people's thinking presumptuous):
1. Format:

do{    loop statement;} while (judging condition);

2. The difference between a while loop statement and a do-while loop statement:
While Loop statement is the first to judge after the execution of the loop statement, Do-while Loop statement is executed first, after judgment. It executes at least once, regardless of whether the condition is satisfied.

3. For example:

1) Analysis Reason: In Java, the Java compiler is not allowed to write nonsense. Because false is a constant, the JVM will recognize the constant value, while (false) is a nonsense, so error.
2) How to solve: the use of variables, the JVM will not recognize, so no longer error.

4, while and do while contrast:

Third, for Loop statement:
1. Format:

for (initialization statement; Judgment statement; post-loop statement) {    loop statement;}

2. The execution flow for the FOR Loop statement:

3. Things to note for the FOR Loop statement:
1) for (;;) This notation is a dead loop statement, which is equivalent to while (true), there can be no three statements in a For loop statement, but the semicolon must not be less and will be error-free.
2) The initialization statement for the FOR Loop statement executes only once, only when the first loop is executed.
3) The Loop body statement for the FOR Loop statement has only one sentence, you can omit the curly brace from writing. However, it is not recommended to omit.

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.