How to Use the Continue Statement in Visual Studio with Examples and Common Pitfalls
A loop often does more work than it needs to. Sometimes one item in a list is invalid, empty, already processed, or simply not relevant. The `continue` statement gives you a clean way to skip that one item and move on without ending the whole loop.
In Visual Studio, `continue` is not a special editor feature. It is a programming statement you write in your code. Visual Studio helps you use it well with IntelliSense, breakpoints, debugging tools, and compiler warnings. Once you understand where `continue` fits, it becomes a simple tool for making loops easier to read.

What the continue statement does
The `continue` statement skips the rest of the current loop iteration and moves control to the next iteration.
That means:
In a `for` loop, execution jumps to the loop update step, such as `i++`, then checks the loop condition again.
In a `while` loop, execution jumps back to the condition.
In a `do while` loop, execution jumps to the condition check at the bottom.
In a `foreach` loop, execution moves to the next item in the collection.
Here is the simplest example in C#:
```csharp
for (int i = 1; i <= 5; i++)
{
if (i == 3)
{
continue;
}
Console.WriteLine(i);
}
```
Output:
```text
1
2
4
5
```
When `i` equals `3`, the `continue` statement runs. The `Console.WriteLine(i);` line is skipped for that iteration only. The loop itself keeps running.
Think of `continue` as saying: “Skip this item, but keep the loop alive.”
That is different from `break`, which stops the loop completely.
```csharp
for (int i = 1; i <= 5; i++)
{
if (i == 3)
{
break;
}
Console.WriteLine(i);
}
```
Output:
```text
1
2
```
Use `continue` when the current item should be ignored. Use `break` when the loop should stop.
How to implement continue in Visual Studio
The steps are simple, but the key is knowing exactly which condition should cause the skip.
Step 1. Open or create a project
Open Visual Studio and create a project that matches the language you want to use. For learning purposes, a C# Console App works well because you can see output right away.
A basic `Program.cs` file might look like this:
```csharp
using System;
class Program
{
static void Main()
{
// Code goes here
}
}
```
Step 2. Add a loop
Choose the loop type that fits your data. For example, use `foreach` when you are processing every item in a collection.
```csharp
string[] names = { "Ava", "", "Mia", "Noah" };
foreach (string name in names)
{
Console.WriteLine(name);
}
```
This prints every value, including the empty one.
Step 3. Add a condition for items to skip
Now decide what makes an item invalid or unwanted. In this case, an empty string should be skipped.
```csharp
foreach (string name in names)
{
if (string.IsNullOrWhiteSpace(name))
{
continue;
}
Console.WriteLine(name);
}
```
Output:
```text
Ava
Mia
Noah
```
The loop still checks every item, but the empty value never reaches `Console.WriteLine`.
Step 4. Keep the condition near the top of the loop
A `continue` statement is easiest to read when it appears early. This pattern is often called a guard clause.
```csharp
foreach (string name in names)
{
if (string.IsNullOrWhiteSpace(name))
{
continue;
}
string formattedName = name.ToUpper();
Console.WriteLine(formattedName);
}
```
The logic reads clearly:
Skip bad data.
Process everything else.
Step 5. Run and test the code
Press F5 to start debugging, or Ctrl+F5 to run without debugging.
Check the output and confirm that:
The skipped values do not appear.
The valid values still process correctly.
The loop ends when expected.
If the behavior looks wrong, place a breakpoint on the `if` statement and inspect the values as the loop runs.

Examples of continue in common programming scenarios
The examples below use C#, but the same idea applies in many languages you can work with in Visual Studio, including C++, JavaScript, and Visual Basic. The syntax may change, but the purpose stays the same.
Skip invalid user input
Imagine you have a list of numbers as strings. Some values are not valid numbers. You want to parse only the valid ones.
```csharp
string[] inputs = { "42", "abc", "17", "", "99" };
foreach (string input in inputs)
{
if (!int.TryParse(input, out int number))
{
continue;
}
Console.WriteLine($"Valid number: {number}");
}
```
Output:
```text
Valid number: 42
Valid number: 17
Valid number: 99
```
This is a good use of `continue` because invalid input is not an error for the whole process. It is just one item to skip.
Process only numbers that match a rule
Suppose you want to print only odd numbers from a range.
```csharp
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 0)
{
continue;
}
Console.WriteLine(i);
}
```
Output:
```text
1
3
5
7
9
```
The condition skips even numbers. The remaining code runs only for odd numbers.
You could also write this with an `if` block:
```csharp
for (int i = 1; i <= 10; i++)
{
if (i % 2 != 0)
{
Console.WriteLine(i);
}
}
```
Both work. Use the version that makes the loop easier to understand. `continue` tends to help when the skip condition is simple and the main logic is longer.
Skip files that do not match an extension
In an app that checks files, you might want to process only `.txt` files.
```csharp
string[] files =
{
"notes.txt",
"photo.jpg",
"todo.txt",
"archive.zip"
};
foreach (string file in files)
{
if (!file.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
{
continue;
}
Console.WriteLine($"Processing text file: {file}");
}
```
Output:
```text
Processing text file: notes.txt
Processing text file: todo.txt
```
This keeps the main file-processing code free from extra nesting.
Use continue in a while loop
`continue` also works inside `while` loops. Be careful with loop variables, because skipping the wrong line can cause an infinite loop.
This version is safe:
```csharp
int count = 0;
while (count < 5)
{
count++;
if (count == 3)
{
continue;
}
Console.WriteLine(count);
}
```
Output:
```text
1
2
4
5
```
The increment happens before `continue`, so the loop can still move forward.
This version is dangerous:
```csharp
int count = 0;
while (count < 5)
{
if (count == 3)
{
continue;
}
count++;
Console.WriteLine(count);
}
```
When `count` becomes `3`, the loop keeps hitting `continue` before `count++` runs. The value never changes, so the loop does not end.
Use continue in nested loops
In a nested loop, `continue` affects the loop it is directly inside.
```csharp
for (int row = 1; row <= 3; row++)
{
for (int column = 1; column <= 3; column++)
{
if (column == 2)
{
continue;
}
Console.WriteLine($"Row {row}, Column {column}");
}
}
```
Output:
```text
Row 1, Column 1
Row 1, Column 3
Row 2, Column 1
Row 2, Column 3
Row 3, Column 1
Row 3, Column 3
```
The `continue` skips only the current iteration of the inner `column` loop. The outer `row` loop keeps going as usual.

Common mistakes to avoid when using continue
`continue` is useful, but it can make code harder to follow when used carelessly.
Confusing continue with break
This is the most common mistake.
`continue` skips the current iteration.
`break` exits the loop.
If your loop stops too early, check whether you used `break` where you meant `continue`.
```csharp
foreach (int number in new[] { 1, 2, 3, 4 })
{
if (number == 2)
{
continue;
}
Console.WriteLine(number);
}
```
This prints `1`, `3`, and `4`.
If you replace `continue` with `break`, it prints only `1`.
Skipping required work
Be careful when the code after `continue` does something required, such as updating a counter, closing a resource, or adding an item to a result.
```csharp
int processed = 0;
foreach (string item in items)
{
if (string.IsNullOrWhiteSpace(item))
{
continue;
}
processed++;
}
```
This counts only non-empty items. That may be correct.
If you meant to count every item checked, move the counter before the `continue`:
```csharp
int checkedCount = 0;
foreach (string item in items)
{
checkedCount++;
if (string.IsNullOrWhiteSpace(item))
{
continue;
}
Console.WriteLine(item);
}
```
The placement matters.
Creating infinite loops
This mostly happens in `while` and `do while` loops.
If `continue` runs before the loop variable changes, the condition may never become false.
Good pattern:
```csharp
while (index < values.Length)
{
string value = values[index];
index++;
if (string.IsNullOrWhiteSpace(value))
{
continue;
}
Console.WriteLine(value);
}
```
The index changes before any possible skip.
Hiding too much logic behind many continue statements
One or two `continue` statements can make a loop cleaner. Too many can make the path through the code hard to trace.
This can become hard to follow:
```csharp
foreach (Order order in orders)
{
if (order == null) continue;
if (order.IsCanceled) continue;
if (order.Total <= 0) continue;
if (!order.IsPaid) continue;
Ship(order);
}
```
This is not always wrong, but it may signal that the condition deserves a name:
```csharp
foreach (Order order in orders)
{
if (!CanShip(order))
{
continue;
}
Ship(order);
}
static bool CanShip(Order order)
{
return order != null
&& !order.IsCanceled
&& order.Total > 0
&& order.IsPaid;
}
```
The loop now reads more like plain English.
Using continue when a filter would be clearer
If you are working with collections in C#, LINQ can sometimes express the same idea more clearly.
```csharp
var activeUsers = users.Where(user => user.IsActive);
foreach (User user in activeUsers)
{
SendEmail(user);
}
```
This can be easier to read than a loop with `continue`, especially when filtering is the main purpose.
That said, `continue` is still a strong choice when the processing logic needs several steps or when you want straightforward debugging inside the loop.
Debugging and improving code that uses continue
Visual Studio gives you several tools to see exactly what happens when a `continue` statement runs.
Place breakpoints around the skip condition
Click in the left margin next to the `if` statement, or place the cursor on the line and press F9.
```csharp
if (string.IsNullOrWhiteSpace(name))
{
continue;
}
```
Run with F5. When the breakpoint hits, hover over variables or check the Locals and Watch windows.
This helps answer questions like:
What value caused the skip?
Is the condition true when it should be false?
Is the loop reaching this code at all?
Step through the loop
Use these common Visual Studio shortcuts:
Shortcut | What it does |
F10 | Steps over the current line |
F11 | Steps into a method call |
Shift+F11 | Steps out of the current method |
F5 | Continues running until the next breakpoint |
When you step over a `continue`, watch where execution moves next. In a `for` loop, it usually moves to the update part of the loop. In a `foreach`, it moves toward the next item.
Use conditional breakpoints
A normal breakpoint stops every time the loop reaches that line. In a large loop, that gets annoying fast.
Right-click a breakpoint and choose Conditions. Then add a condition such as:
```csharp
i == 25
```
or:
```csharp
name == ""
```
Now Visual Studio breaks only when the condition is true. This is very helpful when only one item is being skipped by mistake.
Watch for skipped side effects
If code after `continue` updates state, writes logs, saves records, or changes a variable, check whether that work should happen before the skip.
A helpful habit is to divide loop code into three parts:
Work that must happen for every item.
Conditions that skip some items.
Work that happens only for valid items.
Example:
```csharp
foreach (string record in records)
{
totalRecords++;
if (string.IsNullOrWhiteSpace(record))
{
skippedRecords++;
continue;
}
Process(record);
processedRecords++;
}
```
This version makes the counts clear.
Keep the fast skip near the top
When a loop processes many items, place cheap skip checks before expensive work.
```csharp
foreach (string file in files)
{
if (!file.EndsWith(".json", StringComparison.OrdinalIgnoreCase))
{
continue;
}
string contents = File.ReadAllText(file);
ProcessJson(contents);
}
```
The extension check is much cheaper than reading a file. By skipping early, the code avoids unnecessary work.
Prefer clear conditions over clever ones
A compact condition is not always better.
Harder to read:
```csharp
if (!(user != null && user.IsActive && user.EmailConfirmed))
{
continue;
}
```
Clearer:
```csharp
if (user == null)
{
continue;
}
if (!user.IsActive)
{
continue;
}
if (!user.EmailConfirmed)
{
continue;
}
```
Or use a named method:
```csharp
if (!CanReceiveEmail(user))
{
continue;
}
```
Readable code is easier to debug. It is also easier to change later.

A simple checklist for using continue well
Before adding `continue`, ask these questions:
Does this condition apply only to the current item?
Should the loop keep running after this item is skipped?
Is any required work accidentally placed after `continue`?
In a `while` loop, does the loop variable still change?
Would a named method or collection filter be clearer?
Can a breakpoint confirm that the right items are skipped?
If the answers look good, `continue` is probably a clean fit.
The best use of `continue` is not to make code shorter at any cost. It is to make the main path through a loop easier to see. Put skip conditions near the top, keep them simple, and use Visual Studio’s debugger to confirm your assumptions. With that approach, `continue` becomes a small statement that can remove clutter from loops and make your code easier to maintain.




