Codectionary / Developer documentation / C#

for Loop

The for loop repeats a block of code a specific number of times, controlled by three parts in its header: an initialization (run once), a condition (checked before every iteration), and an update (run after every iteration). It's the standard choice when the number of iterations is known or easily computed in advance, such as looping over an index range.

Syntax

for (initialization; condition; update) {\n  // code\n}

Examples

Basic for Loop

The classic counting loop with initialization, condition, and update.

for (int i = 0; i < 5; i++)
{
    Console.WriteLine(i);  // 0, 1, 2, 3, 4
}

Looping Over an Array by Index

Using a for loop when the index itself is needed, not just the value.

string[] names = { "Fola", "Zain", "Jamal" };

for (int i = 0; i < names.Length; i++)
{
    Console.WriteLine($"{i}: {names[i]}");
}

Nested for Loops

A loop inside another loop, commonly used for working with grids.

for (int row = 1; row <= 3; row++)
{
    for (int col = 1; col <= 3; col++)
    {
        Console.Write(row * col + " ");
    }
    Console.WriteLine();
}

for Loop with Multiple Variables

C#'s for loop can initialize and update more than one variable at once.

for (int i = 0, j = 10; i < j; i++, j--)
{
    Console.WriteLine($"i={i}, j={j}");
}

Best practices

  • Use foreach whenever you only need each element, not its index - it is safer and more readable than a for loop
  • Use a traditional for loop when you need the index, need to iterate backward, or need to skip elements with a custom step
  • Avoid modifying the loop counter variable inside the loop body beyond the update expression - it creates subtle, hard-to-follow bugs
  • Prefer for over while when the number of iterations is known in advance, since it groups all loop control in one place

At a glance

Purpose
Applications on the .NET platform
File extension
.cs
Runs in
.NET runtime
Usually used with
.NET SDK and libraries

Specifications & further reading

Related C# documentation

Variables & Data Types
C# is a statically-typed language, meaning every variable's type is fixed at compile time. Built-in value types include int, double, decimal, bool, and char, while string and object are reference types. The var keyword lets the compiler infer a variable's type from its initializer, but the variable is still strongly typed underneath - it just saves you from writing the type name explicitly.
Console.WriteLine / ReadLine
The Console class, from the System namespace, provides the standard way to read from and write to the terminal in a C# console application. Console.WriteLine() prints a value followed by a newline, Console.Write() prints without one, and Console.ReadLine() reads a full line of text typed by the user, always returning it as a string.
Comments & XML Documentation
C# supports single-line comments with //, multi-line comments with /* */, and a special triple-slash XML documentation format (///) placed above a member. XML doc comments support tags like <summary>, <param>, and <returns>, and are used by Visual Studio and other IDEs to power IntelliSense tooltips and can be compiled into full API documentation.
Arithmetic & Assignment Operators
C# provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder. Integer division truncates any decimal part, just as in many C-family languages. Compound assignment operators (+=, -=, etc.) combine an operation with assignment, and increment/decrement operators (++, --) come in prefix and postfix forms that differ subtly in when the value updates relative to being used.