Loop: You can execute a piece of code repeatedly until the loop condition is not met.
First, the four elements of the cycle: initial conditions, cyclic conditions, state changes, circulation body.
1. Initial conditions: The first state of the cycle.
2. Loop condition: Under what conditions the loop is performed and the condition is not satisfied, the loop terminates.
3. State change: Change the value of the cyclic variable, and ultimately do not meet the cycle conditions, thus stopping the loop.
4. Loop body: The part to be executed repeatedly.
Second, syntax: for syntax.
and While,do...whilte for (initial condition; cyclic condition; state change)
{
Loop body
}
Note: In parentheses after 1.for, three items are separated by semicolons. Do not add a semicolon after the 2.for parenthesis. 3. Do not write the cycle of death. Example: for (int i=1;i<=10;i++) {Console.WriteLine ("Hello");}
Third, the implementation process: 1. Perform the initial conditions 2. Loop condition 3. Loop body 4. Status Change 5 continue to step 2nd.
Iv. Examples of:
1. Find the number within 100 and 7 related. (Can be divisible by 7, single digit is 7, 10 digits is 7)
for (int i=1;i<=100;i++)
{
if (i% 7 = = 0 | | I% = = 7 | | i/10 = = 7)//Key
{
Console.Write (i + "t");
}
}
2. Display the ASCII code of the computer: for (int i=0;i<=125;i++)
{
Console.Write (i+ "=" + (char) i+ "\ t");
}
For Loop variant:
Variant one:
While loop
int i = 1;
Initial conditions for (; I <= 100;)
{
Console.Write (i + "t"); i++;//State Change
}
Equivalent: int i = 1;//initial condition
while (I <= 100)
{
Console.Write (i + "t");
i++;//State Change
}
Variant two: Dead loop for (;;) { }
V. Nesting of loops.
for (int i=1;i<=10;i++)
{
for (int j=1;j<=20;j++)
{
Console.Write ("★");
}
Console.WriteLine ();
}
When the outer layer is cycled 1 times, the inner layers are completely cycled through. Jobs: Print To type:
Row I column J I and J relationship
1 5
2 5
3 5
4 5
5 5
1 1 j<=i
2 2
3 3
4 4
5 5
1 5 j<=6-i
2 4
3 3
4 2
5 1
I empty point
---1 4 1
---2 3 2
--3 2 3
-4 1 4
5 0 5
–
---
--
-
C # cyclic FO statement R