C# For

Learn 'for' loop in C#.

For Loop

A 'for' loop is used to iterate over a range of values or elements. The syntax for a 'for' loop is as follows:

for (initialization; condition; iteration)
{
    // code to be executed
}

Initialization: This part is executed once at the beginning of the loop. It is typically used to initialize a loop control variable.

Condition: It is evaluated before each iteration. If the condition is true, the loop continues; otherwise, it exits.

Iteration: It is executed at the end of each iteration. It is typically used to update the loop control variable.

Here's a simple example of a 'for' loop that prints numbers from 1 to 5:

cs Copy Code
using System;

class Program
{
    static void Main()
    {
        for (int i = 1; i <= 5; i++)
        {
            Console.WriteLine(i);
        }
    }
}
Output:
1
2
3
4
5

Explanation:

Initialization: 'int i = 1' initializes the loop variable 'i' to 1.

Condition: 'i <= 5;' is the loop condition, which means the loop will continue as long as 'i' is less than or equal to 5.

Iteration: 'i++' is the iteration statement, which increments 'i' by 1 after each iteration.

Pre-increment ('++variable'): Increments the variable before using its value in the expression.
Syntax: int y = ++x;

Post-increment ('variable++'): Uses the current value of the variable in the expression and then increments it.
Syntax: int y = x++;

Note: You can customize the initialization, condition, and iteration based on the specific requirements of your loop.

Nested For Loop:

A nested 'for' loop in C# is a loop inside another loop. This allows you to iterate over elements in a multi-dimensional array or perform repetitive tasks in a structured way. Here's an example of a nested 'for' loop:

cs Copy Code
using System;

class Program
{
    static void Main()
    {
        // Example of a 2D array
        int[,] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };

        // Nested for loop to iterate over each element in the 2D array
        for (int i = 0; i < matrix.GetLength(0); i++)
        {
            for (int j = 0; j < matrix.GetLength(1); j++)
            {
                Console.Write(matrix[i, j] + " ");
            }
            Console.WriteLine(); // Move to the next line after each row
        }
    }
}

Explanation:

Outer loop: 'for (int i = 0; i < matrix.GetLength(0); i++)' iterates over the rows of the 2D array.

Inner loop: 'for (int j = 0; j < matrix.GetLength(1); j++)' iterates over the columns of the 2D array.

Console output: 'Console.Write(matrix[i, j] + " ")' statement prints each element followed by a space and the 'Console.WriteLine()' statement is used to move to the next line after each row.

Output:
1 2 3 
4 5 6 
7 8 9 

Remember: You can nest loops as needed based on the structure of your data or the task you want to perform. Ensure that you manage the loop indices and conditions correctly to avoid infinite loops or incorrect results.

What's Next?

We actively create content for our YouTube channel and consistently upload or share knowledge on the web platform.