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($"Runde nummer: {i}"); }

Output:

Runde nummer: 0 Runde nummer: 1 Runde nummer: 2 Runde nummer: 3 Runde nummer: 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:

// Udskriv tallene 1 til 10 for (int i = 1; i <= 10; i++) { Console.WriteLine(i); } // Beregn summen af tallene 1 til 100 int sum = 0; for (int i = 1; i <= 100; i++) { sum += i; } Console.WriteLine($"Summen er: {sum}"); // 5050

Iterating through an array

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

string[] frugter = { "Æble", "Banan", "Appelsin", "Mango" }; for (int i = 0; i < frugter.Length; i++) { Console.WriteLine($"{i + 1}. {frugter[i]}"); }

Output:

1. Æble 2. Banan 3. Appelsin 4. Mango

Tip: frugter.Length gives the number of elements. We use i < frugter.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:

// Udskriv en gangetabel 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 helt ved 5 } Console.WriteLine(i); } // Udskriver: 0, 1, 2, 3, 4

continue — skip the rest of this iteration

for (int i = 0; i < 10; i++) { if (i % 2 != 0) { continue; // Spring ulige tal over } Console.WriteLine(i); } // Udskriver: 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[] frugter = { "Æble", "Banan", "Appelsin" }; // Med for: for (int i = 0; i < frugter.Length; i++) { Console.WriteLine(frugter[i]); } // Med foreach (renere): foreach (string frugt in frugter) { Console.WriteLine(frugt); }

Common mistakes

1. Off-by-one errors

// Fejl: kører 6 gange (0-5) i stedet for 5 for (int i = 0; i <= 5; i++) { ... } // Korrekt: kører 5 gange (0-4) for (int i = 0; i < 5; i++) { ... }

2. Infinite loop

// Glem ikke at opdatere tælleren! for (int i = 0; i < 10; ) // Mangler i++ — kører for evigt { Console.WriteLine(i); }

3. Modifying the counter inside the loop

// Undgå dette — det gør koden svær at forstå for (int i = 0; i < 10; i++) { i += 2; // Forvirrende! Brug step i for-erklæringen i stedet }

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.