In this lesson, you will learn about the “for” loop, how to use it, and its advantage. You will also learn how to exit a loop, and the nested ‘for’ loops.

What is the “For” Loop in C#?

There are many situations when you need to execute a block of statements several number of times in your applications. The ‘for’ loop in C# is useful for iterating over arrays and for sequential processing. This kind of for loop is useful for iterating over arrays and for other applications in which you know in advance how many times you want the loop to iterate. That is the statements within the code block of a ‘for’ loop will execute a series of statements as long as a specific condition remains true.

Every “for” loop defines Initializer, condition, and iterator sections.

A “for” loop is used when you need to execute a statement several numbers of times in your application. The ‘for loop’ is consisted of an initialized, a condition, an increment, and the body of the loop.  The syntax of a loop is represented as the following.

Syntax of ‘for’ Loop

            for (Initializer; condition; increment)
            {
                Loop Body();
            }

1- The initializer is the first thing to get executed in a for loop, and for only one time. It initializes the variable where the loop will start to iterate.

2- The condition checks the current value of the variable against a giving value and executes the loop and perform the execution of the loop body.

3- The increment is to increase the value of the variable by a giving number(most of the time it’s increase by 1, depends on the needs), then rerun the loop until the condition is false. Once the condition is false, the loop will exit to the next statement of the program.


The increment can be a decrement as well, which will decrease the value of the variable in the initializer. This means the loop can go both directions, forwards or backward.

Flowchart of ‘for’ Loop

c# for loop flowchart

Example of C# ‘for’ Loop

Example I: Print numbers from 0 to 10

        static void Main(string[] args)
        {
            for (int i = 0; i <= 10; i++)
            {
                Console.WriteLine(i);
            }
            Console.ReadKey();
        }

Output

C# for loop

Example II: Print numbers from 10 to 0 using decrementing loop

        static void Main(string[] args)
        {
            for (int i = 10; i >= 0; i--)
            {
                Console.WriteLine(i);
            }
            Console.ReadKey();
        }

Output

C# for loop

Break and continue in ‘for’ Loop

We can control the loop iteration by using the break and continue statement. The break statement is to break the loop completely whereas continue skip the current execution and continue to the next iteration.

Example of break

In the example below, the loop will exit when the value of ‘i’ is equal to 5.

        static void Main(string[] args)
        {
            for (int i = 0; i <= 10; i++)
            {
                if (i == 5)
                {
		    Console.WriteLine("Break the loop");
                    break;
                }
                Console.WriteLine(i);
            }
            Console.ReadKey();
        }

Output

C# for loop

Example of continue

In the example below the loop will skip to the next iteration when ‘i’ is equal to 5.

        static void Main(string[] args)
        {
            for (int i = 0; i <= 10; i++)
            {
                if (i == 5)
                {
                    Console.WriteLine("continue to next iteration");
                    continue;
                }
                Console.WriteLine(i);
            }
            Console.ReadKey();
        }

Output

c# loop example

Nested ‘for’ Loops in C#

One or more loops can be inside a loop in C#. It’s useful to use nested loop when looping through metric or DataGrid.

Example of nested ‘for’ Loops

        static void Main(string[] args)
        {
            for (int i = 0; i < 3; i++)
            {
                System.Console.WriteLine("Value of i = " + i);
                for (int j = 0; j < 3; j++)
                {
                    System.Console.WriteLine("Value of j = " + j);
                }
                System.Console.WriteLine("----");
            }

            Console.ReadKey();
        }

Output

for loop example

Last modified: July 21, 2018

Comments

Write a Reply or Comment

Your email address will not be published.