Hit enter after type your search item

Using for loop in C#

The C# for loop is a way to execute a block of code to the specified number of times. The statements inside the for loop keep on executing until the condition is false.

Syntax of using for loop

This is how you may use the for loop in C#:

for ( initialize_expression; condition; iterator ) {

// code to be executed here;


The parts of the for loop in above syntax are explained below:

  • First of all, the initialize_expression is once executed even before entering the for loop. This expression can be the declaration and initialization of a variable. e.g.:

int x = 1;

This variable cannot be accessed from outside of the for loop.

Similarly, the expression can be an object with the new keyword, an await expression, invocation of a method etc.

Note: This is an optional section in for loop.

  • The condition part which is a Boolean expression is also optional in the for loop. For example:

x >= 100;

The loop keeps on executing as long as the condition is evaluated as True or this optional part is not given. As the condition becomes False, the loop exits and control moves to the next line of code outside of the loop.

  • The third section in the for loop is an iterator. This is also optional. There, you may specify what to do after each iteration. For example, increment a variable:

x = x + 1;


Or decrement a variable:

X = x – 1;


Similarly, you may use the assignment statement, await expression, create an object by new keyword or invocate a method there.

An example of for loop with increment operator

In this example, we will display the value of variable x in each iteration of the loop. In the initialize section, the variable x is declared with an initial value of 10.

The second part, condition, is set as the value of variable 100 or less. The third part (iterator) is set to increase the value of x by 10. Have a look at the code and output:

c# for-loop

The example with decrement operator

The example below uses decrement in the iterator section. So, this time variable x value is set as 50 and in each iteration, its will be subtracted by 5. The value of x is displayed in each iteration to show the current value:

c# for-loop-decrement

Using break statement in for loop example

The break statement can be used in the for loop for exiting the loop entirely. As soon as break statement is executed, the for loop quits and control moves to the next line outside of the loop.

Note: If you only want to quit the current iteration rather quitting the loop entirely, use the continue statement (see next example):

In the following example, we have an array of numbers. In the for loop, I used a for loop to iterate through the array. In the condition section, the length method of the array is used.

Inside the loop body, the if condition is used to check the current element value. If this is equal to 10 then break statement will execute to exit the loop:

The result:

c# for-loop-break

Get the leap years by using continue in for loop

As said earlier, for exiting the current iteration only, you may use the continue statement in C#. To demonstrate that, we have an array of years.

By using the for loop with the continue and if statements, we will get the leap years and display on the screen.

The array contains the following years:

{ 1992, 1994, 1996, 2000, 2002, 2008, 2010, 2012, 2016, 2018 }

See the code and how the continue statement is used:

The leap years list:

c# for-loop-continue

You can see, other years are filtered as those encountered the continue statement in for loop.

This div height required for enabling the sticky sidebar