C# for Loop
Here, you will learn how to execute a statement or code block multiple times using the for loop, structure of the for loop, nested for loops, and how to exit from the for loop.
The for
keyword indicates a loop in C#. The for
loop executes a block of statements repeatedly until the specified condition returns false.
for (initializer; condition; iterator)//code block
The for
loop contains the following three optional sections, separated by a semicolon:
Initializer: The initializer section is used to initialize a variable that will be local to a for loop and cannot be accessed outside loop. It can also be zero or more assignment statements, method call, increment, or decrement expression e.g., ++i or i++, and await expression.
Condition: The condition is a boolean expression that will return either true or false. If an expression evaluates to true, then it will execute the loop again; otherwise, the loop is exited.
Iterator: The iterator defines the incremental or decremental of the loop variable.
The following for loop executes a code block 10 times.
for(int i = 0; i < 10; i++)
{
Console.WriteLine("Value of i: {0}", i);
}
</code></pre>
<div className="card-footer example-footer">@ttHelpers.TryItLink("cid=cs-078hIg") </div>
</div>
</div>
<div className="outputLabel">
Output:
</div>
<samp>
Value of i: 0
<br />
Value of i: 1
<br />
Value of i: 2
<br />
Value of i: 3
<br />
Value of i: 4
<br />
Value of i: 5
<br />
Value of i: 6
<br />
Value of i: 7
<br />
Value of i: 8
<br />
Value of i: 9
</samp>
<p>In the above example, <code>int i = 0</code> is an initializer where we define an int variable <code>i</code> and initialize it with 0.
The second section is the condition expression <code>i < 10</code>, if this condition returns <code>true</code> then it will execute a code block. After executing the code block, it will go to the third section, iterator.
The <code>i++</code> is an incremental statement that increases the value of a loop variable <code>i</code> by 1. Now, it will check the conditional expression again and repeat the same thing until conditional expression returns <code>false</code>.
The below figure illustrates the execution steps of the <code>for</code> loop.</p>
<p>
The below figure illustrates the execution steps of the <code>for</code> loop.
</p>
<ResponsiveImage src="/images/csharp/for-loop.webp" alt="for Loop Execution Steps" priority={true} />
<p>If a code block only contains a single statement, then you don't need to wrap it inside curly brackets <code>{ }</code>, as shown below. </p>
<div className="card code-panel">
<div className="card-header example-title">Example: for Loop </div>
<div className="panel-body">
<pre className="csharpcode">
<code>for(int i = 0; i < 10; i++)
Console.WriteLine("Value of i: {0}", i);
</code></pre>
<div className="card-footer example-footer"></div>
</div>
</div>
<p>
An Initializer, condition, and iterator sections are optional. You can initialize a variable before <code>for</code> loop, and condition and iterator can be defined inside a code block, as shown below.
</p>
<div className="card code-panel">
<div className="card-header example-title">Example: for loop C# </div>
<div className="panel-body">
<pre className="csharpcode">
<code>int i = 0;
for(;;)
{
if (i < 10)
{
Console.WriteLine("Value of i: {0}", i);
i++;
}
else
break;
}
Value of i: 1
Value of i: 2
Value of i: 3
Value of i: 4
Value of i: 5
Value of i: 6
Value of i: 7
Value of i: 8
Value of i: 9
Since all three sections are optional in the for
loop, be careful in defining a condition and iterator. Otherwise, it will be an infinite loop that will never end the loop.
for ( ; ; )
{
Console.Write(1);
}
The control variable for the for loop can be of any numeric data type, such as double, decimal, etc.
for (double d = 1.01D; d < 1.10; d+= 0.01D)
{
Console.WriteLine("Value of i: {0}", d);
}
Value of i: 1.02
Value of i: 1.03
Value of i: 1.04
Value of i: 1.05
Value of i: 1.06
Value of i: 1.07
Value of i: 1.08
Value of i: 1.09
The steps part in a for loop can either increase or decrease the value of a variable.
for(int i = 10; i > 0; i--)
{
Console.WriteLine("Value of i: {0}", i);
}
Value of i: 9
Value of i: 8
Value of i: 7
Value of i: 6
Value of i: 5
Value of i: 4
Value of i: 3
Value of i: 2
Value of i: 1
Exit the for Loop
You can also exit from a for loop by using the break
keyword.
for (int i = 0; i < 10; i++)
{
if( i == 5 )
break;
Console.WriteLine("Value of i: {0}", i);
}
Value of i: 1
Value of i: 2
Value of i: 3
Value of i: 4
Multiple Expressions
A for
loop can also include multiple initializer and iterator statements separated by comma, as shown below.
for (int i = 0, j = 0; i+j < 5; i++, j++)
{
Console.WriteLine("Value of i: {0}, J: {1} ", i,j);
}
Value of i: 1, J: 1
Value of i: 2, J: 2
A for
loop can also contain statements as an initializer and iterator.
int i = 0, j = 5;
for (Console.WriteLine($"Initializer: i={i}, j={j}");
i++ < j--;
Console.WriteLine($"Iterator: i={i}, j={j}"))
{
}
Iterator: i=1, j=4
Iterator: i=2, j=3
Iterator: i=3, j=2
Nested for Loop
C# allows a for loop inside another for loop.
for (int i = 0; i < 2; i++)
{
for(int j =i; j < 4; j++)
Console.WriteLine("Value of i: {0}, J: {1} ", i,j);
}
Value of i: 0, J: 1
Value of i: 0, J: 2
Value of i: 0, J: 3
Value of i: 1, J: 1
Value of i: 1, J: 2
Value of i: 1, J: 3