Skip to content
Blog

For loops in C#: Complete guide with examples

|
csharploopsbeginner

For loops in C#

A for loop is one of the most commonly used control structures in C#. It lets you repeat a block of code a specific number of times — perfect for working with lists, counting, or performing the same action multiple times.

Basic syntax

for (int i = 0; i < 5; i++) { Console.WriteLine($"Round number: {i}"); }

Output:

Round number: 0 Round number: 1 Round number: 2 Round number: 3 Round number: 4

A for loop consists of three parts:

  1. Initialization (int i = 0) — runs once at the start
  2. Condition (i < 5) — checked before each iteration; the loop stops when it is false
  3. Update (i++) — runs after each iteration

When do you use a for loop?

Use a for loop when you know how many times you want to repeat something:

// Print the numbers 1 to 10 for (int i = 1; i <= 10; i++) { Console.WriteLine(i); } // Calculate the sum of the numbers 1 to 100 int sum = 0; for (int i = 1; i <= 100; i++) { sum += i; } Console.WriteLine($"The sum is: {sum}"); // 5050

Iterating through an array

One of the most common uses is to iterate through an array:

string[] fruits = { "Apple", "Banana", "Orange", "Mango" }; for (int i = 0; i < fruits.Length; i++) { Console.WriteLine($"{i + 1}. {fruits[i]}"); }

Output:

1. Apple 2. Banana 3. Orange 4. Mango

Tip: fruits.Length gives the number of elements. We use i < fruits.Length (not <=) because arrays start from index 0.

Counting backwards

You can also count downwards:

for (int i = 10; i >= 1; i--) { Console.WriteLine(i); } Console.WriteLine("Start!");

Here we start at 10, check if i >= 1, and subtract 1 after each iteration with i--.

Nested loops (loops within loops)

Sometimes you need a loop inside another loop. This is useful for working with tables or 2D data, for example:

// Print a multiplication table for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) { Console.Write($"{i * j,4}"); } Console.WriteLine(); }

Output:

1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25

Important: Use different variable names in each loop (i and j), otherwise the inner loop will overwrite the outer one.

Break and continue

Two useful keywords to control the flow:

break — stop the loop entirely

for (int i = 0; i < 100; i++) { if (i == 5) { break; // Stop completely at 5 } Console.WriteLine(i); } // Prints: 0, 1, 2, 3, 4

continue — skip the rest of this iteration

for (int i = 0; i < 10; i++) { if (i % 2 != 0) { continue; // Skip odd numbers } Console.WriteLine(i); } // Prints: 0, 2, 4, 6, 8

For loop vs. other loops

C# has several types of loops. Here is a quick comparison:

Loop typeBest for
forWhen you know the number of iterations
foreachWhen you iterate through a collection
whileWhen you don't know how many times
do-whileWhen the code must run at least once

foreach — the easy alternative

When you just need to iterate through a collection without using an index, foreach is often cleaner:

string[] fruits = { "Apple", "Banana", "Orange" }; // With for: for (int i = 0; i < fruits.Length; i++) { Console.WriteLine(fruits[i]); } // With foreach (cleaner): foreach (string fruit in fruits) { Console.WriteLine(fruit); }

Common mistakes

1. Off-by-one errors

// Error: runs 6 times (0-5) instead of 5 for (int i = 0; i <= 5; i++) { ... } // Correct: runs 5 times (0-4) for (int i = 0; i < 5; i++) { ... }

2. Infinite loop

// Don't forget to update the counter! for (int i = 0; i < 10; ) // Missing i++ — runs forever { Console.WriteLine(i); }

3. Modifying the counter inside the loop

// Avoid this — it makes the code hard to understand for (int i = 0; i < 10; i++) { i += 2; // Confusing! Use a step in the for statement instead }

Try it yourself

The best way to learn loops is to write them yourself. Try these exercises:

  1. Print all even numbers from 2 to 20
  2. Calculate the factorial of 10 (10!)
  3. Write a program that prints a triangle of stars

Want instant feedback on your code? In our C# course you can write and run code directly in the browser with automatic grading.

Ready to try it yourself?

Learn to code with interactive exercises and instant feedback — right in the browser.