The for loop in C#

The for loop is one of the available loops in C sharp programming language. The for loop is used to execute a given block of code repeatedly to the given number of times.

In a for loop, you have to set the counter that will be checked in each iteration against a given condition, followed by the increment or decrement operator (counter update).

Following are a few examples of using for C sharp but let us first look at its structure.

Syntax of using the C# for loop

Following is the general syntax of using the for loop in C#:

Where:

  • The for keyword is followed by parenthesis.
  • The counter_initialization sets the initial value of the counter in the for loop which is executed only once.
  • The condition, which is the second statement in for loop, will be evaluated in each iteration. If the condition is true, the given block of code will execute inside the for loop.
  • After that comes an optional update_the_counter part that can be an increment or decrement operator.

Now let us look at a few examples of using for loop.

A simple example of using a C# for loop

Following is an example of using for loop. In this example, I have declared an integer type variable with a value of one. This value is incremented by one in the for loop. Click the following link to see the code and output of the above example.

See online demo and code

You can see, the loop is iterated ten times. As inta variable reaches 11 value and it meets the condition, that turns to be false. As soon as the condition becomes false, the execution will go out of the for loop to the next line, which is Console.ReadLine(); in that case.

A for loop example with decrement operator

As mentioned earlier, you may use increment or decrement operator in the for loop of C#. In this example, the variable is initialized with a value of 10. In each iteration, the value will be decremented by -1. See the output and code by clicking the link below:

See online demo and code

You can see the values of variable inta is displayed from 10 to 1 by using a decrement operator in the counter update of the for loop.

C# continue statement with for loop example

Sometimes it is required to skip one or more iterations in the for loop, however, still keep on running the loop till the end. The continue statement can be used for that purpose.

In this example, I will use continue in the for loop. An int type variable is initiated with the value of 1 and will increment by 1 in each iteration till it reaches the value of 10. As the value of inta variable reaches 4 (after execution) an if condition will check it and we will increment it by 3 followed by the continue statement.

See the output and code by clicking the link below:

See online demo and code

You can see 5,6 and 7 will be omitted in the for loop due to increment in the if statement. While the C# for loop was still iterating by using the continue statement till the condition turned false.

If you need to end the loop as the given condition is met, you may use the break statement. See example below.

C# break statement in the for loop

You can use the break statement of C# to stop the loop and execution will move out of the for loop. See the following example of using break statement.

See online demo and code

You can see, unlike the continue command,  the break statement will stop the for loop and control will be moved to the next line of code, outside of the for loop.