C#

Continue in C#

Introduction to Continue in C#

In the world of programming, controlling the flow of loops is crucial for efficient and error-free execution. The keyword Continue in C# is a control statement that plays a vital role in loop handling. It allows developers to skip the current iteration of a loop and proceed to the next one, based on a specific condition.

This article explores everything you need to know about Continue in C#, including its syntax, behavior in different loops, use cases, and code examples.

What is Continue in C#

The Continue in C#? statement is used to bypass the remaining code inside a loop for the current iteration and jump directly to the next iteration. It is most often used in for, foreach, while, and do-while loops.

Key Characteristics of Continue in C#:

  • Skips the rest of the code in the loop block for the current iteration.
  • Goes back to the loop condition check (for while, for, do-while).
  • Ideal for filtering unwanted conditions.

Syntax of Continue in C#

The syntax is simple and straightforward:

continue;

You place it inside a loop, usually within a conditional if block.

Example:

for (int i = 1; i <= 10; i++) { if (i % 2 == 0) { continue; // Skip even numbers } Console.WriteLine(i); }

Output:

1 3 5 7 9

Explanation:

In this example, the loop prints only the odd numbers between 1 and 10. When i is even, the continue statement skips the Console.WriteLine(i) and moves to the next iteration.

Usage of Continue in C# in Different Loops

Continue in C# with For Loop

for (int i = 0; i < 5; i++) { if (i == 3) continue; Console.WriteLine("i = " + i); }

Output:

i = 0 i = 1 i = 2 i = 4

Continue in C# with While Loop

int i = 0; while (i < 5) { i++; if (i == 3) continue; Console.WriteLine("i = " + i); }

Continue in C# with Do-While Loop

int i = 0; do { i++; if (i == 2) continue; Console.WriteLine("i = " + i); } while (i < 4);

Continue in C# with Foreach Loop

int[] numbers = { 1, 2, 3, 4, 5 }; foreach (int number in numbers) { if (number == 3) continue; Console.WriteLine(number); }

When to Use Continue in C#

You should consider using Continue in C# when:

  • You want to skip processing for specific conditions in a loop.
  • You want cleaner code by avoiding deeply nested if blocks.
  • You need to bypass unwanted logic in certain iterations.

Real-Life Use Case Example

Let’s say you’re processing a list of customer orders but want to skip those with zero quantity.

List<Order> orders = GetOrders(); foreach (var order in orders) { if (order.Quantity == 0) continue; ProcessOrder(order); }

This ensures only valid orders are processed.

Common Mistakes with Continue in C#

  • Placing code after continue; expecting it to run — it won’t.
  • Using continue outside loops — this will cause a compile-time error.
  • Confusing continue with break, which exits the loop entirely.

Difference Between Break and Continue in C#

Feature break continue
Loop Behavior Exits the loop entirely Skips current iteration
Use Case Stop processing completely Skip some cases, continue looping
Applies To Loops & Switch-case Loops only

Best Practices for Using Continue in C#

  • Use it to keep code readable and avoid unnecessary nesting.
  • Do not overuse it—too many continue statements can reduce clarity.
  • Always use meaningful conditions to justify the skip logic.

Conclusion

The Continue in C# statement is a powerful tool when used correctly in loops. It helps in writing clean, optimized, and readable code by skipping unnecessary iterations based on logic. Whether you are filtering data, skipping errors, or simplifying logic, understanding how to properly use Continue in C# will make your C# programming more effective.

line

Copyrights © 2024 letsupdateskills All rights reserved