For-loop.
With this loop we use indexes.
We have a start, and end, and increment expression.
We must then access the element from the array in an expression within the loop.
Here: We use a for-loop to iterate over a string array.
The length of this array is 2, so the valid indexes are 0 and 1.
Info: The variable “i” is set to each array index.
We can access multiple array elements if needed in a for-loop.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | static void Main(string[] args) { string[] array = new string[2]; array[0] = "Java"; array[1] = "Kotlin"; // Use for-loop on array. for (int i = 0; i < array.Length; i++) { // Get element, and print index and element value. string element = array[i]; Console.WriteLine("INDEX: {0}, VALUE: {1}", i, element); } Console.ReadLine(); } |
Output:
