The for-loop
is slightly more complicated than while
and do-while
loops but it can solve more complicated tasks with less code. Here is the scheme describing for-loop
.
for (Initialization; Condition; iterator)
{
loop's body;
}
The for
loop has the following three blocks.
The initialization block initializes a local for loop variable.
i
.The condition is a boolean expression that will return either true
or false
.
The iterator either increment or decrement the loop variable. On each iteration, the counter is updated then the statements are executed.
Let's consider a very simple example of using the for
loop.
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Counter: {0}", i);
}
int i = 0
is an initializer where an int i
is defined and initialized to 0.i < 5
, if it returns true
then it will execute a code block.i
by 1.false
.Let's run the above code and it will print the following output on the console window.
Counter: 0
Counter: 1
Counter: 2
Counter: 3
Counter: 4
Here is another example that is a little bit more complicated.
for (int i = 1, sum = 1; i <= 64; i = i * 2, sum += i)
{
Console.WriteLine("i={0}, sum={1}", i, sum);
}
In the above example, we have two variables i
and sum
, that initially have the value of 1, but we update them consecutively at each iteration of the loop. Let's try this example and you will see the following output.
i=1, sum=1
i=2, sum=3
i=4, sum=7
i=8, sum=15
i=16, sum=31
i=32, sum=63
i=64, sum=127
All the examples related to the for
loop are available in the ForLoop.cs
file of the source code. Download the source code and try out all the examples for better understanding.