C# Loops

while, do-while, for and foreach.

✍️ 2 exercises in this lesson
  • while, do...while
  • for (int i = 0; i < 5; i++)
  • foreach (var item in collection)
  • break and continue

Key Takeaways: C# Loops

Mastering C# Loops helps you write clean and reliable C# code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

C# Loops Code Example

C#
for (int i = 1; i <= 3; i++)
{
    Console.WriteLine($"{i} x 8 = {i * 8}");
}

string[] langs = { "C#", "F#", "VB" };
foreach (var lang in langs)
{
    if (lang == "VB") continue;
    Console.WriteLine(lang);
}

int n = 1;
while (n < 50) n *= 2;
Console.WriteLine(n);
Output
1 x 8 = 8
2 x 8 = 16
3 x 8 = 24
C#
F#
64
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

C# Loops Practice Exercises

Check what you learned in “C# Loops”.

/ 2 solved
Fill in the blank

Loop over every item in a collection.

 (var lang in langs) { }
✓ Correct! Not quite. Try again.
Multiple choice

Which keyword skips to the next iteration?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services