Loop for while do-while

Source: Internet
Author: User


  1. Intialization

  2. Condition loop condition (repeated execution if the condition is met)

  3. Increment increment of cyclic Variables

4. Statement loop body

5.

While (<# condition #> ){
<# Statements #>
}

Initialize the loop variable before the while;

Then, when the condition is followed by the condition in the small Guo: the loop body is executed when the condition is met,

Then statements: Statement (loop body );

At last, write increment: the increment of the loop variable (the increment of the loop variable );



Int I = 1; // the initial value of the cyclic variable.
Do {
Printf ("% d", I); // cyclic body
I ++; // increment of cyclic Variables
} While (I <= 100); // cyclic Condition


/For (INT I = 0; I <101; I ++ ){

// Sum + = I;

//

//} Internal loop. After the sum of all items is executed, the loop is displayed in the output.

// Printf ("sum = % d \ n", sum );

  • Difference between while and do... while loop:

  • For a while loop, you must first judge the loop condition and then execute the loop body. for do .... for a while loop, whether or not the loop condition is true, execute a loop body first, and then judge whether the loop condition is true.

  • Differences between while and for loops:

  • While loops are mostly used when the number of loops is unclear. For loops are mostly used to know the number of loops. For loops can be converted to while loops, but while loops cannot be converted to for loops.

  • When to use a loop?

  • When we do the same thing twice or more times, we need to consider the use of loop. The repeated operations as the loop body.

  • It is recommended that the loop should not exceed two layers, otherwise the readability of the program will deteriorate.

  • // The for loop can be converted to the while and do while loops, but the while and do while loops may not be converted to the for loop.

  • Break is used to end the cycle of the current layer,

  • Continue; // used to end this loop, and then jump to the next loop to continue execution;

This article from "a long time back to one" blog, please be sure to keep this source http://lulun426.blog.51cto.com/9197884/1538384

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.