C # Summary of loop implementation methods,
- C # loop statements: while, for, foreach
1. while Loop
static void Main(string[] args) { int[] hs = { 1,2,3,4,5,6,7,8,9}; int ligh = hs.Length; while (ligh > 0) { Console.WriteLine(hs[ligh - 1]); ligh -= 1; } Console.ReadKey(); }
2. for Loop (nested for loop can be used, for example, during Bubble Sorting)
Static void Main (string [] args) {int [] hs = {1, 2, 3, 4, 5, 6, 7, 8, 9 }; // for reverse printing, you only need to modify the judgment condition for (int I = 0; I
3. foreach cyclically traverses elements in the Set (this method 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) {Console. writeLine (item. toString ();} Console. readKey ();}