Loops are one of the most important control structures in C#.
They allow a program to execute a block of code repeatedly until a specific condition is met.
In real-world applications, loops are used everywhere:
Processing collections of data
Reading records from databases
Iterating over arrays and lists
Repeating tasks until a condition is satisfied
Automating repetitive logic
Along with loops, break and continue statements play a critical role in controlling loop execution.
This article explains all types of loops in C#, along with break and continue, in a clear, descriptive, and practical manner.
What Is a Loop in C#?
A loop is a programming construct that repeatedly executes a block of code as long as a specified condition remains true.
In simple terms:
"Do this task again and again until a condition changes."
Loops help reduce:
Code duplication
Manual repetition
Logical errors
Types of Loops in C#
C# provides the following loop statements:
for loop
while loop
do-while loop
foreach loop
Each loop is designed for specific use cases.
1. for Loop
The for loop is used when the number of iterations is known in advance.
Key Points
Best suited for counter-based iterations.
Initialization, condition, and increment/decrement are defined in one place.
Improves readability when looping a fixed number of times.
Commonly used with arrays and indexed collections.
Syntax
for (initialization; condition; increment/decrement)
{
// loop body
}
Example
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(i);
}
2. while Loop
The while loop executes as long as the condition is true.
Key Points
Used when the number of iterations is not known beforehand.
Condition is checked before executing the loop body.
If the condition is false initially, the loop never runs.
Commonly used for user input validation and continuous checks.
Syntax
while (condition)
{
// loop body
}
Example
int count = 1;
while (count <= 5)
{
Console.WriteLine(count);
count++;
}
3. do-while Loop
The do-while loop executes the loop body at least once, even if the condition is false.
Key Points
Condition is checked after executing the loop body.
Guarantees at least one execution.
Useful when the task must run before validation.
Often used in menu-driven programs.
Syntax
do
{
// loop body
}
while (condition);
Example
int number = 1;
do
{
Console.WriteLine(number);
number++;
}
while (number <= 5);
4. foreach Loop
The foreach loop is used to iterate over collections such as arrays, lists, and dictionaries.
Key Points
No need for index or counter management.
Read-only access to collection elements.
Safer and more readable than for loops.
Ideal for collections and enumerables.
Syntax
foreach (type item in collection)
{
// loop body
}
Example
int[] numbers = { 1, 2, 3, 4, 5 };
foreach (int num in numbers)
{
Console.WriteLine(num);
}
break Statement in C#
The break statement is used to exit a loop immediately, regardless of the loop condition.
Key Points
Terminates the loop completely.
Control moves to the statement after the loop.
Commonly used when a specific condition is met.
Improves performance by stopping unnecessary iterations.
Example
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
break;
}
Console.WriteLine(i);
}
Output: 1 2 3 4
continue Statement in C#
The continue statement skips the current iteration and moves to the next iteration of the loop.
Key Points
Does not terminate the loop.
Skips remaining code in the current iteration.
Useful for ignoring specific values or conditions.
Helps keep logic clean without nested conditions.
Example
for (int i = 1; i <= 5; i++)
{
if (i == 3)
{
continue;
}
Console.WriteLine(i);
}
Output: 1 2 4 5
break vs continue – Key Differences
| Feature | break | continue |
|---|
| Stops loop | Yes | No |
| Skips the current iteration | No | Yes |
| Loop resumes | No | Yes |
| Use case | Exit loop early | Skip specific values |
Nested Loops in C#
A loop inside another loop is called a nested loop.
Key Points
Inner loop runs fully for each outer loop iteration.
Commonly used for matrices, patterns, and grids.
Requires careful control to avoid performance issues.
break affects only the innermost loop.
Example
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 2; j++)
{
Console.WriteLine($"i={i}, j={j}");
}
}
Choosing the Right Loop
| Scenario | Recommended Loop |
|---|
| Fixed number of iterations | for |
| Unknown iterations | while |
| Execute at least once | do-while |
| Collections | foreach |
Common Mistakes with Loops
Infinite loops due to missing condition updates
Forgetting to increment/decrement counters
Using break when continue is required
Modifying collections inside foreach
Overusing nested loops
Loops in Real-World Applications
Loops are used in:
Data processing
File reading
API response handling
Batch jobs
Business rule execution
They form the backbone of program execution.
Loops in C# enable efficient repetition of logic and data processing. By understanding for, while, do-while, and foreach loops—along with break and continue—developers gain full control over program execution flow.
Mastering loops helps you write:
Efficient logic
Clean and readable code
Scalable applications
Loops are a fundamental skill every C# developer must learn thoroughly.
Thank you for reading this detailed guide on Loops in C# with break and continue.
A strong understanding of loops allows you to write powerful, efficient, and maintainable applications.