There are many benefits to using loops - using loops can enable programs to implement judgment logic. With loops, you can use the powerful computing functions of the computer. Below I will list the loop statements in C#, and the code is as follows:
With a loop structure, it is beneficial to use calculations
Powerful computing power
With a loop structure, it is beneficial to use calculations
Powerful computing power
Loop statements in C#: while, for, foreach
1. While loop
static void Main(string[] args) { int[] hs = { 1,2,3,4,5,6,7,8,9}; int ligh = ; while (ligh > 0) { (hs[ligh - 1]); ligh -= 1; } (); }
2. For loop(You can nest for loops, for example: it will be used when doing bubble sorting)
static void Main(string[] args) { int[] hs = { 1,2,3,4,5,6,7,8,9}; //Flashback printing only requires modifying the judgment conditions for (int i = 0; i < ; i++) { (hs[i].ToString()); } (); }
3. Foreach loop through elements in the collection(This writing seems to be unique to .NET)
static void Main(string[] args) { int[] hs = { 1,2,3,4,5,6,7,8,9}; //The var keyword is used here, anonymous type (automatically inferred by the compiler), you can replace it with int foreach (var item in hs) { (()); } (); }
for loop instance
C# for loops are generally used in counting or sorting, so doing is equivalent to numerating each row of data. Therefore, C# for loop occupies an extremely important position in the development process.
int i; for(i=1;i<=10;++i) { ("{0}",i); }
The counter variable is an integer i, whose starting value is 1, incremented by 1 at the end of each loop. During each loop, write the value of i to the console.
Note that when the value of i is 11, the code behind the loop will be executed. This is because at the end of the loop where i is equal to 10, i will increment to 11. This occurs before the test condition i<=10, when the loop ends.
Finally, it is important to note that you can declare the counter variable as part of the C# for loop statement and rewrite the above code as follows:
for(int i=1;i<=10;++i) { ("{0}",i); }
The above content is combined with basic loop statements implemented in C# language and introduced with cases. Friends in need can refer to it. I hope everyone supports me more.