if we want to do a lot of things, say we want to output hundreds of numbers, one output of the words not only cumbersome and time-consuming, inefficient. In this case, C # provides us with a much simpler way to output the loop. There are many types of loops, here I compare two loops: while loop &for Loop.
first of all , the while loop:
the format of the while loop is simple:
While(loop condition)
{
Loop Body
}
This means that when the logical value is "true", the loop body loops continuously until the logical value becomes "false" before it stops. If the logical value is always true, the loop does not stop, which is what we often call a "dead loop". For example:
While(true)
{
Console.Write (1);
}
is to continuously output the number 1. There is no value in such a cycle. If you want to rewrite this code, we do this:
Output of 1 by using the while loop method .
First we can set an integer type variable int i=10,
Int i=10;
while (i>0)
{
Console.Write (i);
I= i-1;
}
Console.ReadLine ();
Then this code is ready to write.
analysis, when executing this program,
first loop: Because the variable i=10 i>0 Yes i=i-1 i 9
second loop: Since the first loop was executed i=9 i>0, Yes i=i-1 i 8
when the loop executes to the 11th time, the variable i=0, does not satisfy the i>0 condition, so is false, then the loop terminates, so the program output Ten a 1 after the stop. This is much easier when you need to output a large number of numbers.
C # Loop statement: While loop with For loop (i)