The for loop format is:
For ([initialization expression]; [condition expression]; [iteration expression ])
{
// Statement Block
}
Here, the [initialization expression], [condition expression], and [iteration expression] are optional. The [condition expression] must be a Boolean expression.
Perform the following steps:
Step 1: run the initialization expression only once.
Step 2: Start to execute the conditional expression (if it is null, return true). If it is true, execute the statement in braces; if it is false, directly jump to the end point of.
Step 3: start executing the iteration expression + condition expression.
Step 4: If the condition expression is true, execute the statement in braces and return step 3. If it is false, the control point is switched to the end point of.
Here we will use multiple examples to deepen our understanding:
Example 1:
| The code is as follows: |
Copy code |
For (int num = 1; num <0; num ++) { Response. Write (num ); }
|
Result: Null.
Cause: execute int num = 1 first, then execute num <0; then num <0 returns false, exit the loop.
Example 2:
| The code is as follows: |
Copy code |
Response. Write ("result: <br/> "); For (int num = 1; num <5; ++ num) { Response. Write (num + "<br/> "); }
|
In the third part, replace with: num ++ and the result is:
Cause: do not always confuse the I ++ and ++ I problems. Use them here, and do not write something similar to int I = 0; I = I ++; I = ++ I. What is I = I ++? It is equivalent:
| The code is as follows: |
Copy code |
Int tmp = 0; I = 1; I = tmp; |
The FOR loop in this example creates a Mandelbrot image.
| The code is as follows: |
Copy code |
Using System; Namespace { Class Program { Public static void Main (string [] args) { Double realCoord, imagCoord; Double realTemp, imagTemp, realTemp2, arg; Int iterations; For (imagCoord = 1.2; imagCoord> =-1.2; imagCoord-= 0.05) { For (realCoord =-0.6; realCoord <= 1.77; realCoord ++ = 0.03) { Iterations = 0; RealTemp = realCoord; ImagTemp = imagCoord; Arg = (realCoord * realCoord) + (imagCoord * imagCoord ); While (arg <4) & (iterations <40 )) { RealTemp2 = (realTemp * realTemp)-(imagTemp * imagTemp)-realCoord; ImagTemp = (2 * realTemp * imagTemp)-imagCoord; RealTemp = realTemp2; Arg = (realTemp * realTemp) + (imagTemp * imagTemp ); Iterations + = 1; } Switch (iterations % 4) { Case 0: Console. Write ("."); Break; Case 1: Console. Write ("o "); Break; Case 2: Console. Write ("0 "); Break; Case 3: Console. Write ("@"); Break; } } Console. Write ("n "); } Console. ReadKey (); } } } |
What I saw in the group before is quite good. Post it
| The code is as follows: |
Copy code |
Class Program { Static void Main (string [] args) { // List <int> items = new List <int> (); // For (int I = 0; I <100; I ++) //{ // Items. Add (I ); //} // For (int I = 0; I <items. Count; I ++) //{ // Items. RemoveAt (I ); //} // Console. WriteLine (items. Count ); // Console. ReadKey (); // List <int> items = new List <int> (); // For (int I = 0; I <100; I ++) //{ // Items. Add (I ); //} // Int itemCount = items. Count; // For (int I = 0; I <itemCount; I ++) //{ // Items. RemoveAt (I ); //} // Console. WriteLine (items. Count ); // Console. ReadKey (); // Var items = new List <int> (); // For (var I = 0; I <100; I ++) //{ // Items. Add (I ); //} // Items. RemoveAll (item => item> 50 ); // Console. WriteLine (items. Count ); // Console. ReadKey (); // Var items = new List <int> (); // For (var I = 0; I <100; I ++) //{ // Items. Add (I ); //} // Foreach (var item in items) //{ // Items. Remove (item ); //} // Console. WriteLine (items. Count ); // Console. ReadKey (); Var items = new List <int> (); For (var I = 0; I <100; I ++) items. Add (I ); Var items2 = items; Foreach (var item in items2) { Items. Remove (item ); } Console. WriteLine (items. Count ); Console. WriteLine (items2.Count ); Console. ReadKey (); } } |