In the process of writing the C # language, we often encounter various need loops, but how do we choose the three commonly used loops in the C # language (while loop, do While loop, for loop)?
In the first while loop, the write format of the while loop is:
while (loop condition) ... (The Loop condition includes: A value variable expression, but it must be of type bool.) )
{
Loop body ... (Circular body refers to: any function, any number of code.) If there is only one line of code, you can omit the curly brackets. )
}
Note: When the loop body is executed, the system will first determine the cycle conditions, if the condition is true, then execute the loop body, after the completion of the loop body, then judge, Judge True, then continue to execute the loop, until the judgment is false, the end of the loop.
Next is the Do and loop, which is written in the following format:
Do
{
Loop body ... (Circular body refers to: any function, any number of code.) If there is only one line of code, you can omit the curly brackets. )
}
while (cyclic condition); (The Loop condition includes: A value variable expression, but it must be of type bool.) )
Note: When the loop body is executed, the system executes the loop body first, then determines whether the loop condition is satisfied, and if so, executes the loop body again, and then ends the loop if the condition is satisfied until the condition is not satisfied.
The last is the most commonly used for loop, and its writing format is:
for (expression 1; loop condition; expression 2) ... (expression 1 can be any code; Expression 2 can be any type, executed after the loop body executes)
{
Loop body
}
Note: For loop execution, the loop condition is first judged to be true, if true, the loop body is executed, the expression 2 is run after execution, and the loop condition is judged until the loop condition is false.
So in the C # language to write about the loop body code, not busy writing code, and should carefully analyze the conditions, see the application of that kind of loop body, so that will not write half to find not applicable, and then re-write, so also have to organize ideas, waste a lot of time. I hope this essay can help everyone.
Three common loops used in C # language writing code