YouTip LogoYouTip

Csharp For Loop

[![Image 1: C# Loops](#) C# Loops](#) A **for** loop is a repetitive control structure that allows you to write a loop that executes a specific number of times. The syntax for a **for** loop in C# is: for ( init; condition; increment ){ statement(s);} Here is the control flow of a for loop: 1. **init** is executed first, and only once. This step allows you to declare and initialize any loop control variables. You can also omit writing any statement here, as long as a semicolon appears. 2. Next, the **condition** is evaluated. If it is true, the loop body is executed. If it is false, the loop body is not executed, and the control flow jumps to the next statement immediately following the for loop. 3. After executing the for loop body, the control flow jumps back to the **increment** statement above. This statement allows you to update the loop control variable. This statement can be left empty, as long as a semicolon appears after the condition. 4. The condition is evaluated again. If it is true, the loop is executed, and this process repeats (loop body, then increment step, then re-evaluate condition). The for loop terminates when the condition becomes false. ![Image 2: For Loop in C#](#) ## Example using System; namespace Loops { class Program { static void Main(string[] args) { /* for loop execution */ for(int a =10; a <20; a = a +1) { Console.WriteLine("a Value: {0}", a); } Console.ReadLine(); } } } When the above code is compiled and executed, it produces the following result: a Value: 10 a Value: 11 a Value: 12 a Value: 13 a Value: 14 a Value: 15 a Value: 16 a Value: 17 a Value: 18 a Value: 19 ### foreach C# also supports the foreach loop, which can be used to iterate over an array or a collection object. The foreach loop in C# can be used to traverse collection types, such as arrays, lists, dictionaries, etc. It is a simplified version of the for loop, making the code more concise and readable. Here is the syntax for the foreach loop: foreach (var item in collection){ // loop} collection is the collection to iterate over, and item is the current element being iterated. The following example has three parts: * Output the elements of an integer array using a foreach loop. * Output the elements of an integer array using a for loop. * A counter for setting array elements in a foreach loop. ## Example class ForEachTest { static void Main(string[] args) { int[] fibarray =new int[]{0, 1, 1, 2, 3, 5, 8, 13}; foreach(int element in fibarray) { System.Console.WriteLine(element); } System.Console.WriteLine(); // Similar to foreach loop for(int i =0; i < fibarray.Length; i++) { System.Console.WriteLine(fibarray); } System.Console.WriteLine(); // Setting a counter for elements in the collection int count =0; foreach(int element in fibarray) { count +=1; System.Console.WriteLine("Element #{0}: {1}", count, element); } System.Console.WriteLine("Number of elements in the array: {0}", count); } } The output is: 011235813011235813Element #1: 0Element #2: 1Element #3: 1Element #4: 2Element #5: 3Element #6: 5Element #7: 8Element #8: 13Number of elements in the array: 8 In the following example, we use foreach to traverse a list: ## Example using System; using System.Collections.Generic; class Program { static void Main(string[] args) { // Create a list of strings List myStrings =new List(); // Add some string elements to the list myStrings.Add("Google"); myStrings.Add(""); myStrings.Add("Taobao"); // Use a foreach loop to traverse the elements in the list foreach(string s in myStrings) { Console.WriteLine(s); } // Wait for the user to press any key before exiting the program Console.ReadKey(); } } When the above code is compiled and executed, it produces the following result: GoogleTutorialTaobao For more foreach content, refer to: [Usage of foreach traversal in C#](#) [![Image 3: C# Loops](#) C# Loops](#)
← Csharp Do While LoopCsharp While Loop β†’