For Loops

  • Iterates a set number of times.

// Iterated 10 times, i increased by 1 every time
for (int i = 0; i < 10; i++)
{
    Console.WriteLine(i);
}
  • Increment by two

// Iterated 5 times, i increased by 2 every time
for (int i = 0; i < 10; i+= 2)
{
    Console.WriteLine(i);
}
  • Decreasing the counter

// Iterated 10 times, i decreased by 1 every time
for (int i = 10; i >= 0; i--)
{
    Console.WriteLine(i);
}
  • Be mindful about the increment/decrement and the condition checks. Could easily run into an infinite loop situation.

Last updated