C# For loop with Examples

In c#, for loop is useful to execute a statement or a group of statements repeatedly until the defined condition returns true.

 

Generally, for loop is useful in c# applications to iterate and execute the certain block of statements repeatedly until the specified number of times.

Syntax of C# For Loop

Following is the syntax of defining for loop in c# programming language.

 

for (initialization; condition; iterator(inc / dec))

{

// Statements to Execute

}

If you observe above syntax, we defined a for loop with 3 parts those are initialization, condition, iterator and these are separated with semicolon (;).

 

  1. In initialization part, the variable will be declared and initialized. The initialization part will be executed only once at the starting of the for loop.
  2. After completion of initialization part, the condition part will be evaluated. Here condition is a boolean expression and it will return either true or false.
  3. In case, if condition is evaluated to true
    • The statements inside of for loop will be executed.
    • After that, the iterator part will be executed and it will increase or decrease the initialized variable value based on our requirements.
    • After changing the variable value, again the condition will be evaluated and execute the statements within the loop.
    • This process will continue until the condition is evaluated to false.
  4. If condition is evaluated to false, then the for loop execution will be stopped and control will come out of the loop.

For example, if we have a for loop to print the variable (i) value for 4 times, then the process flow of for loop will be like as shown below.

 

C# For Loop Process Flow Diagram

 

As discussed, first we declared and assigned a value (1) to the variable i, then the condition (i <= 4) will be evaluated. Since, the condition is true, then the statements within the loop will be executed.

 

After that, the iterator (i++) will be evaluated and it will increase the value of variable (i). Again the condition (i <= 4) checking will happen and it will continue until the condition return true.

 

Here the condition will return true till variable i value become 4, after that if i value become 5, then the condition (5 <= 4) will fail and it return false

C# For Loop Flowchart Diagram

Following is the pictorial representation of for loop process flow diagram in c# programming language.

 

C# For Loop Flow Chart Diagram

 

Now we will see how to use for loop in c# programming language with examples.

C# For Loop Example

Following is the example of using for loop in c# programming language to iterate or loop through a particular list of statements.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            for (int i = 1; i <= 4; i++)

            {

                Console.WriteLine("i value: {0}", i);

            }

            Console.WriteLine("Press Enter Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above code, we defined a for loop to iterate over 4 times to print the value of variable i and following are the main parts of for loop.

 

  • Here int i = 1 is the initialization part
  • i <= 4 is the condition part
  • i++ is the iterator part

When we execute above c# program, we will get the result like as shown below.

 

C# For Loop Example Result

 

If you observe above result, for loop executed for 4 times and printed the variable i value for 4 times.

C# For Loop with Multiple Variables

In c# for loop, we can declare and initialize multiple variables and iterator expressions by separating with comma (,) operator.

 

Following is the example of using multiple variables and iterator expressions in c# for loop.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            for (int i = 1, j = 0; i <= 4; i++, j++)

            {

                Console.WriteLine("i: {0}, j: {1}", i, j);

            }

            Console.WriteLine("Press Enter Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we defined a two variables (i, j) and two iterator expressions (i++, j++) by separating them with comma (,) operator.

 

When we execute above c# program, we will get the result like as shown below.

 

C# For Loop with Multiple Variables Example Result

 

This is how we can use multiple variables and multiple iterators in c# for loops based on our requirements.

C# For Loop with Break Statement

In c#, by using break keyword we can stop the execution of for loop statement based on our requirements.

 

Following is the example of stop the execution of for loop using break statement.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            for (int i = 1; i <= 4; i++)

            {

                if (i == 3)

                    break;

                Console.WriteLine("i value: {0}", i);

            }

            Console.WriteLine("Press Enter Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above code, we used a break statement to exit for loop whenever the variable i value equals to 3.

 

When we execute above c# program, we will get the result like as shown below.

 

C# For Loop with Break Statement Example Result

 

If you observe above result, whenever the variable i value equals to 3, then automatically the for loop execution has stopped.

 

This is how we can we use break statement in for loop to terminate the execution of for loop based on our requirements.

C# For Loop without Initialization & Iterators

Generally, the initializer, condition and iterator parameters are optional to create for loop in c# programming language.

 

Following is the example of creating a for loop in c# programming language without initializer and iterator.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            int i = 1;

            for ( ; i <= 4; )

            {

                i++;

                Console.WriteLine("i value: {0}", i);

            }

            Console.WriteLine("Press Enter Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we defined a for loop without any initializers and iterators.

 

When we execute above c# program, we will get the result like as shown below.

 

C# For Loop without Initialization & Iterators Example Result

 

 This is how we can implement for loop in c# programming language without initializers and iterators based on our requirements.

C# Infinite For Loop

In case, if the condition parameter in for loop always returns true, then the for loop will be an infinite and runs forever. Even if we miss the condition parameter in for loop automatically that loop will become an infinite loop.

 

Following are the different ways to make for loop as an infinite loop in c# programming language.

 

for (initializer; ; iterator) {

// Statements to Execute

}

 

or

 

for ( ; ; )

{

// Statements to Execute

}

Following is the example of making a for loop as an infinite in c# programming language.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            for (int i = 1; i > 0; i++)

            {

                i++;

                Console.WriteLine("i value: {0}", i);

            }

            Console.WriteLine("Press Enter Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above code, the condition (i > 0) whatever we defined in for loop will always returns true so it will return infinite result.

C# Nested For Loop

In c#, we can create one for loop within another for loop based on our requirements. Following is the example of creating a nested for loop in c# programming language.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            for (int i = 1; i <= 4; i++)

            {

                for (int j = i; j < 3; j++)

                {

                    Console.WriteLine("i value: {0}, j value: {1}", i, j);

                }

            }

            Console.WriteLine("Press Enter Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we created a for loop within another loop and printing the values based on our requirements.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Nested For Loop Example Result

 

This is how we can create nested for loops in our c# programming language based on our requirements.