C# for Loop

Updated on     Kisan Patel

The for loop statement is one of the most widely used control statement. The for loop executes a statement in the loop until the given condition is true.

The basic structure of a for loop is –

for(initialization; condition; increments/decrements) {
   statement or block of code
}

Lets take one example to understand how to use for loop in C# program.

using System;

namespace ConsoleApp
{
    class forloopDemo
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 10; i++)
            {
                Console.WriteLine(i);
            }
        }
    }
}

The output of the above C# program…

c-sharp-for-loop-demo

  • In above example, the integer variable i is initialized with the value of 1.
  • The statements in the for loop are executed while the condition i<=10 remains true.
  • The value of i is incremented i++ by 1 each time the loop starts.

C#

Leave a Reply