C# For Loop

Looping in C# is a mechanism in which some instructions are repeatedly executed until a particular condition reaches. This C# programming language supports looping mechanisms like while, do while, for loop, and foreach.

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

If we know the range of how many times the loop should execute, then we use the C# for loop. If we don’t know the range, we use a while and do while. For example, if we want to print odd numbers from 1 to 10. We know that the iteration 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 or do while to iterate the tables until the data will. fetch.

In C#, every for 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 C# for loop 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 Syntax

The C# For loop is a range-based loop, i.e., we can use for loop when we know how often the iteration has to repeat. Within the for loop syntax, all three sections’ initialization, condition check, and 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 working is the same as the while. 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 iteration. 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 C# for loop. It then continues the execution of the statements after the closing brace of it.

C# For Loop Example

Let us see a C# 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 iteration 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 and checks if i ==10 which returns false.

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

i incremented i.e., i = 2. The control enters 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 and checks if i == 10, which returns true. So the break statement is executed, and control comes out of it.

Categories C#