Loop execution, application, and common errors

Source: Internet
Author: User

There are 3 loops--while loops, for loops, and do...while loops. The following sections describe the execution and applicability of each cycle.

1. While loop

(1) Suitable situation: Suitable for a new variable is not needed to control whether the loop stops.

(2) Execution process: first determine whether the conditions in the parentheses immediately after the condition is true (generally non-0 value), for the real time to execute the loop body, until the condition is false without executing the loop body, jump directly to the code behind the while loop body.

(3) Common error: The loop body does not change the value of the condition variable or the loop body no break statement or RETURN statement (continue statement cannot jump out of the loop) is called the Dead loop, never terminates the loop. When a semicolon (;) is appended to the () of a while statement, it is called an empty statement, when the loop body executes only once.

2. For loop

(1) Suitable situation: applicable to the cycle of known times, a new variable is required to control the number of times the loop body executes.

(2) Execution procedure: The For statement is in the form of for (statement one; statement two; statement three) {loop body}. Statement one is executed first, and is typically assigned to a new variable (that is, a variable that controls the number of times the loop body executes). Then execute the conditional judgment of statement two, for the true time to execute the loop body, to terminate the execution of the loop body when false. The loop body is executed once for real, and then the statement three (typically a new variable (i.e. a variable that controls the number of times the loop body executes)) is changed, and the missing statement three or loop body does not have the statement three will become a dead loop. Next go back to statement two of the judgment to continue the execution process just now.

(3) Common error: a semicolon (;) followed by a for statement is the same as a common error in a while statement. The sentence in the for loop is often confused, do not know how to fill in order to cycle the corresponding number of times. This will require you to write your own control variable to the number of times the statement two is false, depending on the situation. Note that statement two can not be omitted, otherwise it will be an error. Do not write the statement II as any control variable is the case, or become a dead loop.

3. Do...while Cycle

(1) Suitable situation: apply to the first cycle of the body, and then to determine the situation.

(2) Execution process: first to perform a cycle body, and then judge the conditions. When the condition is true, the loop body is executed again ... Know that the condition is false.

(3) Common error: Should not first execute the loop body, and hard to use do...while cycle. The semicolon following the parentheses () of the Do...while () statement (;) cannot be omitted, which is distinguished from the parentheses of the while statement.

The break statement is to jump out of the nearest loop statement, Loop statement, Loop statement, not a SELECT statement (except for the switch statement, especially if, else, else if statements). The continue statement is not to execute the code following the continue statement until the end of the most recent loop statement ("}"). The continue statement in for IS jumping to statement three, down to the judgment of the statement two ... while statements are rare, if a statement that does not have the ability to change a condition before the continue statement in a while statement is a dead loop.

The output function (printf) is often placed in a loop statement, where multiple results are output, unless a break statement is encountered to terminate the loop.

Loop execution, application, and common errors

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.