</>
Skip to content
C# lessons (16/34)

C# — Loops

for loop

using System;

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

foreach loop

using System;
using System.Collections.Generic;

// Array
int[] nums = { 1, 2, 3, 4, 5 };
foreach (int n in nums)
{
    Console.Write($"{n} ");
}
Console.WriteLine();

// List
var names = new List<string> { "Alice", "Bob", "Charlie" };
foreach (string name in names)
{
    Console.WriteLine(name);
}

// With index (use for loop or LINQ)
foreach (var (name, index) in names.Select((n, i) => (n, i)))
{
    Console.WriteLine($"{index}: {name}");
}

while loop

using System;

int count = 0;
while (count < 5)
{
    Console.Write($"{count} ");
    count++;
}
Console.WriteLine(); // 0 1 2 3 4

do-while loop

using System;

int count = 0;
do
{
    Console.Write($"{count} ");
    count++;
} while (count < 5);
Console.WriteLine(); // 0 1 2 3 4

break and continue

using System;

// break: exit the loop
for (int i = 0; i < 100; i++)
{
    if (i == 5) break;
    Console.Write($"{i} ");
}
Console.WriteLine(); // 0 1 2 3 4

// continue: skip to next iteration
for (int i = 0; i < 10; i++)
{
    if (i % 2 == 0) continue;
    Console.Write($"{i} ");
}
Console.WriteLine(); // 1 3 5 7 9

Labeled break/continue

using System;

// Break out of nested loops
for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 5; j++)
    {
        if (i * j > 10)
        {
            Console.WriteLine($"\nStopping at i={i}, j={j}");
            goto done;
        }
        Console.Write($"{i * j} ");
    }
    Console.WriteLine();
}
done:
Console.WriteLine("Done");

Infinite loops

using System;

// while (true)
int i = 0;
while (true)
{
    if (i >= 5) break;
    Console.Write($"{i} ");
    i++;
}
Console.WriteLine();

Collection expressions

using System;
using System.Linq;

// Range operator
foreach (int n in 0..5)
{
    Console.Write($"{n} ");
}
Console.WriteLine(); // 0 1 2 3 4

// Reverse range
foreach (int n in 4..0)
{
    Console.Write($"{n} ");
}
Console.WriteLine(); // 4 3 2 1

Common patterns

using System;

// Sum of numbers
int sum = 0;
for (int i = 1; i <= 100; i++)
{
    sum += i;
}
Console.WriteLine($"Sum: {sum}"); // 5050

// Find first divisible by 7
int n = 51;
while (n % 7 != 0) n++;
Console.WriteLine($"First > 50 divisible by 7: {n}"); // 56

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

Mini Practice

Write C# code that:

  1. Uses a for loop to print even numbers 1-20
  2. Uses foreach to iterate a dictionary
  3. Uses while to find the first Fibonacci number > 1000
  4. Uses nested loops to print a right triangle of asterisks

Up Next

In the next lesson, you'll learn about Arrays — working with arrays and collections.

Related Topics

Frequently Asked Questions about Loops

What is Loops in C#?

Loops is a fundamental concept in C#. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Loops?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Loops.

Why is Loops important in C#?

Loops is essential for C# development. Understanding this concept will help you write better code and solve real-world problems more effectively.