What is a 'for' Loop in C#?

A foundational guide to the 'for' loop in C#, a fundamental control flow statement for executing a block of code a specific number of times. Learn the classic syntax and how it gives you precise control over iteration.

In programming, a loop is a structure that repeats a block of code. The for loop is one of the most common and versatile looping constructs in C#. It's typically used when you know in advance how many times you want the loop to run.

The classic C# for loop gives you precise control over the iteration process by combining three parts into a single, clear line.

The for Loop Syntax

A for loop is defined by three expressions separated by semicolons, all enclosed in parentheses:

for (initializer; condition; iterator)

  1. Initializer: This expression runs only once, before the loop starts. It's used to declare and initialize a loop counter variable (e.g., int i = 0).
  2. Condition: This boolean expression is checked before each iteration. If it's true, the loop body executes. If it's false, the loop terminates.
  3. Iterator: This expression is executed at the end of each iteration. It's typically used to increment or decrement the loop counter (e.g., i++).

A Simple Example: Counting to 5

for (int i = 0; i < 5; i++)
{
    Console.WriteLine($"The current number is {i}");
}

Output:

The current number is 0
The current number is 1
The current number is 2
The current number is 3
The current number is 4

Here's the flow:

  1. int i = 0 runs once. i is now 0.
  2. The condition i < 5 (0 < 5) is checked. It's true.
  3. The loop body runs, printing "The current number is 0".
  4. The iterator i++ runs. i is now 1.
  5. The condition i < 5 (1 < 5) is checked. It's true.
  6. The loop body runs, printing "The current number is 1".
  7. This continues until i becomes 5. The condition i < 5 (5 < 5) is now false, and the loop terminates.

Iterating Through an Array or List

A common use for a for loop is to iterate through the elements of an array or a list, especially when you need access to the index of each element.

var names = new List<string> { "Alice", "Bob", "Charlie" };

for (int i = 0; i < names.Count; i++)
{
    Console.WriteLine($"Person at index {i} is {names[i]}");
}

When to Use a for Loop

A for loop is the right choice when:

  • You need to loop a specific, known number of times.
  • You need access to the loop counter variable (the index).
  • You need more control over the iteration, such as counting backward, skipping items, or changing the counter in a non-standard way.

For simply iterating over every item in a collection without needing the index, the foreach loop is often a cleaner and more readable choice. However, the for loop provides more power and flexibility.

Common Variations

Counting Down:

for (int i = 5; i > 0; i--)
{
    Console.WriteLine(i);
}

Iterating with a different step:

// Print even numbers from 0 to 10
for (int i = 0; i <= 10; i += 2)
{
    Console.WriteLine(i);
}

Conclusion

The for loop is a fundamental control structure in C# and many other programming languages. It provides a robust and flexible way to execute a block of code repeatedly. By giving you explicit control over the initializer, condition, and iterator, it allows you to handle a wide variety of looping scenarios, from simple counting to complex array manipulation. It's an essential tool for every C# developer.