Skip to content
C# Studies

Loop - FOR

Base structure

  • It has 3 parts:
    • Counter
    • Condition
    • Increment

Let’s look at the following example:

for (int i = 0 ; i <= 5 ; i++)
  • int i = 0 is the counter, starting at zero
  • i <= 5 is the condition, which runs until the value reaches 5
  • i++ is the increment, where the value of i goes up one by one

Other examples:

for (var i = 0 ; i <= 5 ; i++) // Counts from 0 to 5
for (var i = 0 ; i < 5 ; i++) // Counts from 0 to 4
for (var i = 5 ; i >= 0 ; i--) // Counts from 5 down to 0

Now the complete example of a For that prints the value of i

for (int i = 0 ; i <= 5 ; i++)
{
Console.WriteLine(i); // Every time it gets here it prints i
}
// OUTPUT:
// 0
// 1
// 2
// 3
// 4
// 5

For each

With arrays we can use a different kind of for called foreach, which lets us iterate over the values stored in an array

Let’s look at the following array:

int[] numbers = { 10, 20, 30, 40 };

We want to work with the values inside this array. Instead of choosing a position, we can iterate over all of its values

int[] numbers = { 10, 20, 30, 40 };
foreach (int num in numbers)
{
Console.WriteLine($"Number: {num}"); // prints every value
}