The continue statement in C# is used to skip the remaining statements in the current iteration of a loop and immediately proceed to the next iteration. It works in loops such as for, while, do-while, and foreach.
1. In for Loops The continue statement skips the rest of the loop body and proceeds with the next iteration.
Example:
for (int i = 0; i < 10; i++)
{
if (i % 2 == 0) // Skip even numbers
{
continue;
}
Console.WriteLine(i);
}
Output
1
3
5
7
9
Explanation:
When i % 2 == 0, the continue statement skips the Console.WriteLine(i) and moves to the next iteration.
2.In while Loops The continue statement skips to the next iteration by reevaluating the loop condition.
int i = 0;
while (i < 10)
{
i++;
if (i % 2 == 0) // Skip even numbers
{
continue;
}
Console.WriteLine(i);
}
1
3
5
7
9
3.In do-while Loops The continue statement skips the remaining code in the loop body and reevaluates the condition.
int i = 0;
do
{
i++;
if (i == 5) // Skip when i equals 5
{
continue;
}
Console.WriteLine(i);
} while (i < 10);
1
2
3
4
6
7
8
9
10
4.In foreach Loops The continue statement skips the rest of the current iteration and moves to the next item in the collection.
string[] names = { "Alice", "Bob", "Charlie", "David" };
foreach (var name in names)
{
if (name.StartsWith("C")) // Skip names starting with 'C'
{
continue;
}
Console.WriteLine(name);
}
Alice
Bob
David
Key Points
1. Skip Current Iteration:
2. Works in Loops Only:
3. Difference Between break and continue:
break exits the loop entirely.
Example
for (int i = 0; i < 5; i++)
{
if (i == 3)
{
break; // Exit the loop when i equals 3
}
Console.WriteLine(i);
}
// Output: 0, 1, 2
for (int i = 0; i < 5; i++)
{
if (i == 3)
{
continue; // Skip iteration when i equals 3
}
Console.WriteLine(i);
}
// Output: 0, 1, 2, 4
Common Use Cases
The continue statement in C# is used to skip the remaining statements in the current iteration of a loop and immediately proceed to the next iteration. It works in loops such as for, while, do-while, and foreach.
1. In for Loops The continue statement skips the rest of the loop body and proceeds with the next iteration.
Example:
for (int i = 0; i < 10; i++) { if (i % 2 == 0) // Skip even numbers { continue; } Console.WriteLine(i); }
Output
1 3 5 7 9
Explanation:
When i % 2 == 0, the continue statement skips the Console.WriteLine(i) and moves to the next iteration.
2.In while Loops The continue statement skips to the next iteration by reevaluating the loop condition.
int i = 0; while (i < 10) { i++; if (i % 2 == 0) // Skip even numbers { continue; } Console.WriteLine(i); }
1 3 5 7 9
3.In do-while Loops The continue statement skips the remaining code in the loop body and reevaluates the condition.
int i = 0; do { i++; if (i == 5) // Skip when i equals 5 { continue; } Console.WriteLine(i); } while (i < 10);
1 2 3 4 6 7 8 9 10
4.In foreach Loops The continue statement skips the rest of the current iteration and moves to the next item in the collection.
string[] names = { "Alice", "Bob", "Charlie", "David" }; foreach (var name in names) { if (name.StartsWith("C")) // Skip names starting with 'C' { continue; } Console.WriteLine(name); }
Alice Bob David
Key Points
1. Skip Current Iteration:
2. Works in Loops Only:
3. Difference Between break and continue:
break exits the loop entirely.
Example
for (int i = 0; i < 5; i++) { if (i == 3) { break; // Exit the loop when i equals 3 } Console.WriteLine(i); } // Output: 0, 1, 2 for (int i = 0; i < 5; i++) { if (i == 3) { continue; // Skip iteration when i equals 3 } Console.WriteLine(i); } // Output: 0, 1, 2, 4
Common Use Cases
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