The C# Continue Statement is one of the most important loop control statements in C# programming. When working with C# loops, developers often need a way to skip the current iteration and move directly to the next iteration of the loop. That is exactly where the continue keyword in C# becomes useful.
In this detailed guide, you will learn everything about the C# continue statement, including syntax, flow control, real-world examples, usage in different loops, best practices, performance considerations, and common mistakes. This content is specially designed for learners and developers who want to master C# programming basics, control flow statements in C#, and C# loop statements.
The continue statement in C# is a loop control statement that skips the remaining code inside the loop body for the current iteration and jumps to the next iteration of the loop.
It is commonly used inside:
When the continue keyword is executed:
The syntax of the continue statement in C# is very simple:
continue;
It must be used inside a loop. If used outside a loop, it will cause a compilation error.
To understand C# loop control statements, it is important to understand the execution flow:
The C# for loop continue is one of the most common usages. It is helpful when filtering values during iteration.
using System;
class Program
{
static void Main()
{
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 0)
{
continue;
}
Console.WriteLine(i);
}
}
}
Output: 1 3 5 7 9
Explanation:
The C# while loop continue works similarly but developers must be careful to update loop variables properly to avoid infinite loops.
using System;
class Program
{
static void Main()
{
int i = 0;
while (i < 10)
{
i++;
if (i == 5)
{
continue;
}
Console.WriteLine(i);
}
}
}
In this example:
The continue keyword in C# can also be used inside a do-while loop.
using System;
class Program
{
static void Main()
{
int i = 0;
do
{
i++;
if (i == 3)
{
continue;
}
Console.WriteLine(i);
} while (i < 5);
}
}
Here, when i becomes 3, the output skips printing 3 and continues execution.
The C# foreach loop continue is useful when working with arrays, lists, and collections.
using System;
class Program
{
static void Main()
{
string[] names = { "John", "", "Alice", null, "Bob" };
foreach (string name in names)
{
if (string.IsNullOrEmpty(name))
{
continue;
}
Console.WriteLine(name);
}
}
}
This example filters out empty and null values using the continue statement.
Understanding the difference between break vs continue in C# is very important.
for (int i = 1; i <= 5; i++)
{
if (i == 3)
break;
Console.WriteLine(i);
}
Output: 1 2
for (int i = 1; i <= 5; i++)
{
if (i == 3)
continue;
Console.WriteLine(i);
}
Output: 1 2 4 5
Skipping invalid inputs in user-driven applications.
Skipping unwanted records in arrays, lists, or database results.
Reducing nested conditions and improving readability.
Skipping inactive objects in update loops.
Ignoring corrupted lines while reading files.
int i = 0;
while (i < 5)
{
if (i == 2)
{
continue;
}
i++;
}
This causes infinite loop because i is not incremented before continue.
Using too many continue statements reduces code readability.
This causes compile-time error.
When using C# continue statement inside nested loops, it affects only the inner loop.
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
if (j == 2)
{
continue;
}
Console.WriteLine("i = " + i + ", j = " + j);
}
}
The continue statement only skips iteration of inner loop when j equals 2.
The continue keyword in C# does not significantly impact performance. It simply changes execution flow. However, using it wisely improves:
The C# Continue Statement is a powerful control flow statement used to skip current loop iteration and continue with the next iteration. It is widely used in C# programming, especially in C# loop statements, data filtering, validation, and real-world application development.
Mastering the continue keyword in C# improves logical thinking and helps write cleaner, more efficient code.
C# is primarily used on the Windows .NET framework, although it can be applied to an open source platform. This highly versatile programming language is an object-oriented programming language (OOP) and comparably new to the game, yet a reliable crowd pleaser.
The C# language is also easy to learn because by learning a small subset of the language you can immediately start to write useful code. More advanced features can be learnt as you become more proficient, but you are not forced to learn them to get up and running. C# is very good at encapsulating complexity.
The decision to opt for C# or Node. js largely hinges on the specific requirements of your project. If you're developing a CPU-intensive, enterprise-level application where stability and comprehensive tooling are crucial, C# might be your best bet.
C# is part of .NET, a free and open source development platform for building apps that run on Windows, macOS, Linux, iOS, and Android. There's an active community answering questions, producing samples, writing tutorials, authoring books, and more.
Copyrights © 2024 letsupdateskills All rights reserved