Summary: in this tutorial, you’ll learn how to use the C# for loop statement to execute a block repeatedly.
Introduction to the C# for loop statement
C# for
statement executes a block while a condition is true
. The following shows the syntax of the for
statement:
for(initializer; condition; iterator)
{
// statement
}
Code language: C# (cs)
The for
statement has the following elements initializer
, condition
, iterator
, and the loop body
initializer
The for
statement executes the initializer
only once before entering the loop. Typically, you declare and initialize a local loop variable in the initializer.
Note that if you declare a variable in the initializer, you cannot access it outside the for
statement.
condition
The condition
is a boolean expression that determines whether the for
statement should execute the next iteration.
The for
statement evaluates the condition
before each iteration. If the condition
is true
(or is not present), the for
statement executes the next iteration. Otherwise, it’ll exit the loop.
iterator
The for
statement executes the iterator
after each iteration.
loop body
The loop body is inside the curly braces ({}
) that may consist of one or more statements. If the condition
is true
(or not present), the for
statement executes the loop body in each iteration.
In the for
statement, the initializer
, condition
, and iterator
are optional. Therefore, you can have an indefinite for
loop like this:
for(;;)
{
// statement
}
Code language: C# (cs)
In this case, you need to use the break
statement to terminate the loop at some point in time to avoid an indefinite loop.
The following flowchart illustrates how the for
loop statement works.
In practice, you’ll use the for
statement to execute a block a specified number of times.
C# for loop examples
Let’s take some examples of using the for
loop statement.
1) A simple C# for loop example
The following example uses the for
statement to output three numbers from zero to two to the console:
for (int i = 0; i < 3; i++)
{
Console.WriteLine(i);
}
Code language: C# (cs)
Output:
0
1
2
Code language: C# (cs)
How it works.
In the initializer, we declare and initialize the local variable i
:
int i = 0
Code language: C# (cs)
In this condition, we check if the variable i
is less than three. The condition will determine if the next iteration should run:
i < 3
Code language: C# (cs)
Since the variable i
is zero, which is less than three, the for
statement executes its loop body that outputs the variable i
to the console:
Console.WriteLine(i);
Code language: C# (cs)
After the first iteration, the for
statement runs the iterator that increases the value of the variable i
by one:
i++
Code language: C# (cs)
The i
variable is 1.
Starting from the second iteration, the for
statement doesn’t execute the initializer but only evaluates the condition to determine whether it should continue to the next iteration.
i < 3
Code language: C# (cs)
Because the variable i
is one, the condition is true
. Therefore, the for
statement executes its loop body the second time that outputs the variable i to the console, and executes the iterator that increases the variable i by one:
i++
Code language: C# (cs)
After this, the variable i
is two. The for
statement continues checking the condition to determine if it should continue to the next iteration:
i < 3
Code language: C# (cs)
Because variable i
is two, the condition is true
. Therefore, the for
statement runs the third iteration that outputs the variable i
to the output and increases the variable i
by one.
This time the i
variable is three that made the condition false
. Hence, the loop exits.
In summary, the for
statement runs its body three times that output three numbers zero, one, and two to the console.
2) Using the C# for loop to calculate the total of integers from 1 to 10
The following example uses the C# for
statement to calculate the total of integers from 1 to 10:
int total = 0;
for (int i = 1; i <= 10; i++)
{
total += i;
}
Console.WriteLine($"Total:{total}");
Code language: C# (cs)
Output:
Total:55
Code language: C# (cs)
In this example, the for
statement adds the variable i
to the total
in each iteration.
3) Using the C# for loop to display even numbers in the range
The following example uses the for
loop statement to display even numbers between 0
and 10
:
for (int i = 0; i < 10; i++)
{
if (i % 2 == 0)
{
Console.Write($"{i} ");
}
}
Code language: C# (cs)
Output:
0 2 4 6 8
Code language: C# (cs)
In this example, the loop body only displays the variable i
if it is an even number determined by the condition i%2 == 0
.
4) Using the C# for loop to display every character of a string
The following example uses the for
statement to display every character of a string:
string message = "Hello";
for (int i = 0; i < message.Length; i++)
{
Console.WriteLine(message[i]);
}
Code language: C# (cs)
Output:
H
e
l
l
o
Code language: C# (cs)
In this example, the string message
has an index starting from 0
to message.Length - 1
. Therefore, we use the for
statement to access individual characters of the string by an index in that range.
Summary
- Use the
for
loop to execute a block repeatedly for a specified number of times.