Loop statements in C #: while LOOP and for loop (2 ),

Source: Internet
Author: User

Loop statements in C #: while LOOP and for loop (2 ),

The while loop is written in the previous article. This article will write a for loop. Compared with the while LOOP, The for loop is a little more complex, but more intuitive than the while loop.

The for loop format is:

For (expression 1; loop condition; expression 2)

{

Loop body

}

Expression 1 is generally used to define variables. If the loop condition is "true", the loop continues. Otherwise, the loop ends. If it is null, it becomes an endless loop; expression 2 is generally used for auto-increment or auto-subtraction of variables. For example, if you output 100 Hello entries in the console and use the for loop to implement them, it is like this:

For (int I = 0; I <100; I ++)

{

Console. Write ("hello ");

}

If you use the while loop to implement it, as follows:

Int I = 0;

While (I <100)

{

Console. Write ("hello ");

I ++;

}

Compared with the two loop methods, it is obvious that the for loop is simpler to write than the while LOOP code. There is no limit on the loop method to choose. It is best to be concise and intuitive when writing code.

 

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.