C# for loop

The C# for loop executes a block of code repeatedly until the condition is false. This type of loop is useful when the number of loop iterations is fixed (predetermined), and the counter variable that determines how many times the loop is going to be executed is needed in the loop.

The structure of a C# for loop is as follows:

C# for loop execution order C# for loop flow diagram

The C# for loop starts with the for keyword followed by three expressions separated by semicolons within parentheses.

  • The first parameter declares and initializes a counter variable before the loop is executed and is always evaluated once at the start.
  • The second parameter is a condition (Boolean expression) and is evaluated before each new iteration of the loop. This must be true in order for the next iteration to be performed. The iterations end when the condition is false.
  • The third parameter is an iterator (incrementer or decrementer), which prepares the loop for the next iteration. This is evaluated after each iteration at the bottom of the loop, usually incrementing the loop counter declared and initialized as a first parameter.
  • Statements are the block of code that you want to execute multiple times in a loop.

for Loop with a Single Statement

The following example demonstrates usage of C# for loop. It writes out all the integers from 0 to 4.

Run Demo

The above code consists of:

  1. When the loop is first encountered, C# initializes the counter variable i with the int type and assigns an initial value of 0 to it (i=0) as shown in the initialization part above.
  2. A Boolean condition (i < 4) will repeatedly execute until the counter is less than 4.
  3. An expression for updating the counter (i++ meaning i=i+1) adds 1 at the end of each repetition of the loop, thus increasing the counter variable by 1.


With a Block Statements

If you have more than one statement within the loop, you must enclose the statements in the opening and closing braces.

for (int i = 0; i < 4; i++)
   Console.WriteLine("Some code executing here.");

Run Demo

Infinite C# for Loop

Whenever you don’t give a condition to the loop, it automatically becomes an infinite loop, meaning the loop block is executed endlessly. Use the double semicolon (;;) in for loop to make it run infinite times as shown below.

Note: To stop the loop, press CTRL + C on the keyboard.

for (;;)
  Console.WriteLine("I am infinite for loop");

Run Demo

Nested C# for Loop

A loop contained within another loop body is a nested loop. The innermost loop is executed more times than the outermost. There is no limitation on the number of loops in another, which means that you can combine different types of loops in your programs.

An inner loop can appear within an outer loop, this way:

First, initialization of the first for loop begins and then the execution of its body will start, which contains the second for loop. Each iteration of the first loop triggers the second loop, which executes until the condition of the inner loop returns false. After that, the second iteration of the outer for loop starts, and the whole inner loop will be performed once again. This process repeats until the outer loop finishes, which makes the inner loop fully execute as many times as the body of the outer loop.

Of course, a break within either the inner or outer loop would interrupt this process.

Let’s see how two nested loops look like:

for (int i = 1; i <= 4; i++)
   for (int j = 1; j <= 2; j++)
     Console.WriteLine(i + ", " + j);

Run Demo


1, 1
1, 2
2, 1
2, 2
3, 1
3, 2
4, 1
4, 2

Loop Control Statements

You may need to use jump statements to control the order in which the statements are executed. Read more on …

C# Reference | Microsoft Docs

User Rating 0 (0 vote)
Tagged in :
Send this to a friend