C# For Loop Example

  • C#
  • 1 min read

In C#, Do While Loops, and in While Loops to iterate, we need to initialize a variable (i = 0), specify the logical condition (i < 10), and need to increment the value of that variable (i++). The For Loop combine all these steps in a single loop statement. Below are its syntax and example:

Syntax

for ([initializers]; [expression]; [iterators]) statement

Example

using System;

namespace ForLoopExample
{
    class ForLoopExample
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 100; i++)
            {
                Console.Write("{0} ", i);
                if (i % 10 == 0)
                {
                    Console.WriteLine("\t{0}", i);
                }
            }
            Console.ReadLine();
            return;
        }
    }
}

Output

i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8
i: 9

See also: