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.