Understanding Loops in C# - Part 1
Milos Tanaskovic
Senior Frontend Engineer | Software & System Development | JavaScript & TypeScript Ecosystem | System Design and Architecture | C# & .NET Ecosystem | Helping & Mentoring junior|mid developers! Ex-pro Basketball Player??
Loops are an important part of C# programming. They allow us to run the same set of instructions repeatedly until a certain condition is met. Learning how to use loops will make your code more efficient and easier to understand. In this guide, I'll explore the different types of loops in C# and when to use them.
What Are Loops in C#?
In C#, loops help us repeat tasks, go through lists of items, and perform actions multiple times without writing the same code over and over. There are four main and primary types of loops in C#:
1. for
2. while
3. do-while
4. foreach
Each loop type works in a unique way and serves different purposes.
1. The for Loop
The for loop is used when you know exactly how many times you want to repeat something. It consists of three parts:
- Start: Sets the beginning value.
- Condition: Checks if the loop should keep running.
- Update: Changes the value after each round.
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Number: " + i);
}
When to Use:
- When the number of repetitions is known in advance.
- For counting or processing items with a fixed size.
2. The while Loop
The while loop runs as long as the condition is true. It's useful when the number of repetitions isn't known beforehand.
int counter = 0;
while (counter < 5)
{
Console.WriteLine("Count: " + counter);
counter++;
}
When to Use:
- When waiting for a condition to change.
- When the number of loops depends on user input or external factors.
领英推荐
3. The do-while Loop
The do-while loop is similar to the while loop, but it always runs at least once before checking the condition.
int count = 0;
do
{
Console.WriteLine("Count: " + count);
count++;
} while (count < 5);
When to Use:
- When you need the loop to run at least once.
- When creating menus or asking for user input.
4. The foreach Loop
The foreach loop is used to go through all the items in a list or collection without needing a counter.
string[] fruits = {"Apple", "Banana", "Cherry"};
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
When to Use:
- When processing all elements in a collection.
- When working with arrays, lists, or dictionaries.
Summary
In this guide, I have covered the four main and primary types of loops in C#:
- for: Use when the number of repetitions is known.
- while: Use when looping depends on a condition.
- do-while: Use when at least one iteration is needed.
- foreach: Use when looping through a collection.
In the next parts, we'll explore advanced loop techniques and ways to optimize performance.
Stay tuned for Part 2, where I am going to dive into making loops more efficient and applying them in real-world projects!
P.S. Want to become Next-Gen Software Engineer? ????????
Subscribe to the branded new Next-Gen Software Engineer newsletter and get two practical articles every week.
Follow me on LinkedIn.