C# For Loop

Looping in C# is a mechanism in which some instructions repeatedly execute until a particular condition reaches. C# supports looping mechanisms like while, do while, for loop, foreach loop

In C# programming, For loop is a range based loop. While loop and do while loops are condition-based loops.

If we know the range of how many times the loop should execute, then we use the for loop. If we don’t know the range, we use a while loop and do while loop. For example, if we want to print odd numbers from 1 to 10. We know that the loop should execute ten times, so we go with for loop. 

But there will be a case where we have to fetch particular data from the database. In that case, we don’t know how many records are available in the database. So we have to use a while loop or do while loop to iterate the tables until the data fetched.

In C#, every loop will have three sections

Initialization: It is nothing but assigning a value to a variable.

Syntax:

<Datatype> <variable name> = value;

Ex: int i = 10;

Condition: This condition compares two or more values or expressions, and it will return a Boolean value.

Ex: 5 <= 7 – 2; which returns true.

Increment/Decrement: Incrementing or decrementing a variable value.

Ex:

int i = 5;

i++; it will increment i by 1.

i = i – 2; it will decrement i by 2.

C# For Loop

The C# For loop is a range-based loop, i.e., we can use for loop when we know how often the loop has to repeat. Within the C# for loop syntax, all the three sections initialization, condition check, and the increment/decrement will be in the same line with comma separation.

for(<initialization>; <boolean expression>; <increment/decrement>)
{
    Statements;
}

Apart from the C# for loop syntax, the for loop’s working is the same as the while loop. It means that once the variable is get initialized, it will check for the boolean expression. If the boolean expression evaluates to true, it enters the loop. It then executes the statements inside the loop and the next Increment/decrement of that variable. Again checks for the condition.

This process repeats until the boolean expression evaluates to true. Once it returns false, control comes out of the loop. It then continues the execution of the statements after the closing brace of the loop.

C# For Loop Example

Let us see an example code using for loop to print even numbers from 0 to 10.

using System;

namespace CSharp_Tutorial
{
    class Program
    {
        static void Main()
        {
            for (int i = 0; i <= 10; i++)
            {
                if (i == 10)
                    break;

                if (i % 2 != 0)
                    continue;

                Console.Write("{0} ", i);
            }
            Console.ReadLine();
        }
    }
}

OUTPUT

C# For Loop 1

In this C# For loop example, i is an integer variable initialized with 0. The control enters the loop and checks if i==10, which returns false.

It checks 0 % 2 !=0 is false, hence continue is not executed and prints 0.

i incremented, i.e., i = 1. The control enters the loop and checks if i ==10 which returns false.

It checks 1 % 2 !=0, that is true, hence continue is executed and skips the print statement.

i incremented i.e., i = 2. The C# control enters the loop and checks if i==10, which returns false. Next, it checks 2 % 2 !=0 is false, hence continue is not executed and prints 2.

……..

When i increments 10, the control enters the loop and checks if i == 10, which returns true. So the break statement is executed, and control comes out of the loop.