Skip to main content

For Loops

A for loop repeats a block of code a set number of times. Use it when you know how many times to repeat before the loop starts, for example:

  • Print the numbers from 1 to 10.
  • Count down from 10 before a rocket launch.
  • Ask the user for 5 grades.

Diagram of a for loop

Syntax​

A for loop keeps everything that controls the counter on one line:

for (initialization ; condition ; recurring)
{
// Code block
}
  1. The initializer runs ONCE, before anything else (e.g. int i = 0).
  2. The condition is checked before each pass. If it's false, the loop ends.
  3. If it's true, the code block runs.
  4. The iterator runs (e.g. i++), then the program jumps back to step 2.

Like a while loop, the condition is checked first, so the block may run zero times.

For vs. While​

Any for loop can be written as a while loop.

These two programs do the same thing:

// while loop
int i = 0;
while (i < 3)
{
Console.WriteLine("hello");
i++;
}

// equivalent for loop
for (int i = 0; i < 3; i++)
{
Console.WriteLine("hello");
}

The for loop keeps the counter's start, end, and step in one place, which makes it easier to read (and why we use it mostly tbh)

  • Known number of passes 👉 use for.
  • Repeat until something happens 👉 use while.

Example 1: Counting up​

Print the numbers 0 to 4.

for (int i = 0; i < 5; i++)
{
Console.WriteLine("i = " + i);
}
PartCodeMeaning
initializerint i = 0Start the counter at 0
conditioni < 5Keep going while i is under 5
iteratori++Add 1 after each pass

i < 5 starting from 0 runs exactly 5 times. This 0 to < n pattern is the most common for loop you'll write.

Example 2: Counting down​

For loops can also be iterated in a decreasing manner:

for (int apples = 10; apples > 6 ; apples --)
{
Console.WriteLine("miam apples");
}

When counting down, the condition usually uses > (or >=). When counting up, it usually uses < (or <=).

Common Mistakes​

Changing the counter inside the loop​

The for loop already updates i in the iterator. Changing it again inside the block makes the loop hard to follow and can skip passes or never end.

for (int i = 0; i < 5; i++)
{
i++; // ❌ i now goes up by 2 each pass
}

Using the counter outside the loop​

A variable declared in the initializer only exists inside the loop.

for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
Console.WriteLine(i); // ❌ error: i does not exist here

Off-by-one errors​

< and <= change how many times the loop runs.

for (int i = 0; i < 5; i++) // runs 5 times (0 to 4)
for (int i = 0; i <= 5; i++) // runs 6 times (0 to 5)

Infinite Loops​

The iterator must move the counter toward making the condition false.

for (int i = 0; i < 5; i--) // ❌ i goes down, so it's always < 5
{
Console.WriteLine(i);
}

A semicolon after the parentheses​

for (int i = 0; i < 5; i++); // ❌ the ; is an empty loop body
{
Console.WriteLine("hello"); // runs only once
}

Leaving parts empty​

This compiles, but it's just a while loop in disguise. If you don't need all three parts, use while instead.

int grape = 0;
for ( ; grape < 3; )
{
Console.WriteLine("hello");
grape++;
}