C # iterative statement,
1. do while statement
The do statement repeats the statement or statement block until the specified expression is false. If the loop body is a single statement, it can not be placed in {}. If not, it must be placed in. The following code
Codeint I = 0; do {Console. writeLine (I); I ++} whie (I <10); do {Console. writeLine (I); while (I <5 );
If {} is not added, it will be executed in an infinite loop.
Note: The do while statement is executed once no matter whether the while condition is true or false.
2. while statement
The while statement executes a statement or block until the specified expression is false. The following statement
Codeint n = 1; while (n <6) {Console. WriteLine (n); n ++ ;}
The while statement is different from the do while statement. It can only be executed when the while condition is true. It will not be executed once like do while.
When a break, goto, return, or throw statement transfers control beyond a while loop, the loop can be terminated. To pass control to the next iteration without exiting the loop, use the continue statement.
3. for statement
The for statement is executed repeatedly until the specified expression is false.
The structure of the for statement is as follows:
Codefor (initializer; condition; iterator) body
Of course, the three parameters in the for statement can be empty.
4. foreach in statement
The foreach statement repeats a set of embedded statements for each element in the array or object set that implements the System. Collections. IEnumerable or System. Collections. Generic. IEnumerable <T> interface. The foreach statement is used to access the set cyclically to obtain the information you need, but cannot be used to add or remove items in the source set. Otherwise, unpredictable side effects may occur. To add or remove items in the source set, use the for loop.
The embedded statement continues to be executed as an array or each element in the set. After completing Iteration for all elements in the Set, control the next statement passed to the foreach block.
You can use the break keyword to jump out of the loop at any point in the foreach block, or use the continue keyword to enter the next iteration of the loop.
The foreach loop can also exit through the goto, return, or throw statements.
The following method uses the foreach statement to access the array:
Codeint [] numbers = {4, 5, 6, 1, 2, 3,-2,-1, 0}; foreach (int I in numbers) {System. console. write (I );}