C#  

Loops in C# – A Complete Guide with break and continue

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:

  1. for loop

  2. while loop

  3. do-while loop

  4. 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

  1. Best suited for counter-based iterations.

  2. Initialization, condition, and increment/decrement are defined in one place.

  3. Improves readability when looping a fixed number of times.

  4. 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

  1. Used when the number of iterations is not known beforehand.

  2. Condition is checked before executing the loop body.

  3. If the condition is false initially, the loop never runs.

  4. 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

  1. Condition is checked after executing the loop body.

  2. Guarantees at least one execution.

  3. Useful when the task must run before validation.

  4. 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

  1. No need for index or counter management.

  2. Read-only access to collection elements.

  3. Safer and more readable than for loops.

  4. 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

  1. Terminates the loop completely.

  2. Control moves to the statement after the loop.

  3. Commonly used when a specific condition is met.

  4. 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

  1. Does not terminate the loop.

  2. Skips remaining code in the current iteration.

  3. Useful for ignoring specific values or conditions.

  4. 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

Featurebreakcontinue
Stops loopYesNo
Skips the current iterationNoYes
Loop resumesNoYes
Use caseExit loop earlySkip specific values

Nested Loops in C#

A loop inside another loop is called a nested loop.

Key Points

  1. Inner loop runs fully for each outer loop iteration.

  2. Commonly used for matrices, patterns, and grids.

  3. Requires careful control to avoid performance issues.

  4. 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

ScenarioRecommended Loop
Fixed number of iterationsfor
Unknown iterationswhile
Execute at least oncedo-while
Collectionsforeach

Common Mistakes with Loops

  1. Infinite loops due to missing condition updates

  2. Forgetting to increment/decrement counters

  3. Using break when continue is required

  4. Modifying collections inside foreach

  5. 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.