Aral Code

Control Flow Lesson 8 of 15

C# Loops — for, foreach, while and do-while

Repeat code in C# with for, foreach, while and do-while loops, use break and continue, validate input in a loop, and build nested patterns.

C# has four loops: for for counting, foreach for going through collections, and while and do-while for repeating until something changes.

for

for (int i = 1; i <= 5; i++)
{
    Console.WriteLine($"7 x {i} = {7 * i}");
}

Output

7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35

foreach

string[] officers = { "Ana", "Ben", "Carla" };
foreach (string name in officers)
{
    Console.WriteLine($"Officer: {name}");
}

int sum = 0;
foreach (int g in new[] { 88, 92, 79 })
{
    sum += g;
}
Console.WriteLine($"Total: {sum}");

Output

Officer: Ana
Officer: Ben
Officer: Carla
Total: 259

foreach reads each item in turn. You can't change the collection (add or remove items) while looping through it with foreach.

while

decimal savings = 0;
int weeks = 0;
while (savings < 2500m)
{
    savings += 300m;
    weeks++;
}
Console.WriteLine($"{weeks} weeks, saved {savings}");

Output

9 weeks, saved 2700

do-while and input validation

int grade;
do
{
    Console.Write("Enter grade (0-100): ");
} while (!int.TryParse(Console.ReadLine(), out grade) || grade < 0 || grade > 100);

Console.WriteLine($"Saved: {grade}");

Output

Enter grade (0-100): ninety
Enter grade (0-100): 120
Enter grade (0-100): 93
Saved: 93

The loop body runs at least once, then repeats while the input is invalid. This is the standard way to make console input "student-proof".

A menu loop

decimal balance = 1000m;
while (true)
{
    Console.Write("[1] Balance [2] Withdraw [0] Exit: ");
    string choice = Console.ReadLine() ?? "";
    if (choice == "0") break;
    if (choice == "1")
    {
        Console.WriteLine($"Balance: {balance:N2}");
    }
    else if (choice == "2")
    {
        Console.Write("Amount: ");
        if (decimal.TryParse(Console.ReadLine(), out decimal amt) && amt > 0 && amt <= balance)
            balance -= amt;
        else
            Console.WriteLine("Invalid amount.");
    }
}
Console.WriteLine("Thank you!");

Output

[1] Balance [2] Withdraw [0] Exit: 2
Amount: 1500
Invalid amount.
[1] Balance [2] Withdraw [0] Exit: 2
Amount: 250
[1] Balance [2] Withdraw [0] Exit: 1
Balance: 750.00
[1] Balance [2] Withdraw [0] Exit: 0
Thank you!

break and continue

for (int n = 1; n <= 20; n++)
{
    if (n % 3 == 0) continue;   // skip multiples of 3
    if (n > 10) break;          // stop after 10
    Console.Write($"{n} ");
}
Console.WriteLine();

Output

1 2 4 5 7 8 10

Nested loops

int rows = 4;
for (int i = 1; i <= rows; i++)
{
    Console.Write(new string(' ', rows - i));
    Console.WriteLine(new string('*', 2 * i - 1));
}

Output

   *
  ***
 *****
*******

new string('*', 5) creates a string of five stars, which saves an inner loop.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which loop is best for going through every item in an array?
2. How many times does a do-while body run if the condition is false at first?
3. What does new string('-', 3) create?
4. Can you add items to a List while looping through it with foreach?

By Sir Reyel · Last updated 2026-12-04