Introduction
The continue
statement in C# is used to skip the remaining code inside the current iteration of a loop and proceed with the next iteration. It is useful when you want to skip certain iterations based on a condition without terminating the entire loop.
Syntax
In a for
Loop
for (initialization; condition; iteration)
{
if (someCondition)
{
continue;
}
// Code to execute in the loop
}
In a while
Loop
while (condition)
{
if (someCondition)
{
continue;
}
// Code to execute in the loop
}
In a do-while
Loop
do
{
if (someCondition)
{
continue;
}
// Code to execute in the loop
} while (condition);
Example: Using continue in a for Loop
Here’s a complete example demonstrating the use of the continue
statement in a for
loop in C#:
using System;
namespace ContinueInForLoopExample
{
class Program
{
static void Main(string[] args)
{
// Using a for loop to print numbers from 1 to 10
// but skip numbers that are multiples of 3
for (int i = 1; i <= 10; i++)
{
if (i % 3 == 0)
{
continue; // Skip the current iteration
}
Console.WriteLine("Number: " + i);
}
// This will always be executed
Console.WriteLine("Loop finished.");
}
}
}
Output
Number: 1
Number: 2
Number: 4
Number: 5
Number: 7
Number: 8
Number: 10
Loop finished.
Explanation
- Initialization:
for (int i = 1; i <= 10; i++)
initializes the loop counteri
to 1 and specifies the loop condition and iteration. - Condition Check:
if (i % 3 == 0)
checks ifi
is a multiple of 3. - Continue Statement:
continue;
skips the current iteration wheni
is a multiple of 3. - Loop Body:
Console.WriteLine("Number: " + i);
prints the current value ofi
.
Example: Using continue in a while Loop
This example demonstrates the use of the continue
statement in a while
loop to skip even numbers:
using System;
namespace ContinueInWhileLoopExample
{
class Program
{
static void Main(string[] args)
{
int counter = 0;
// Using a while loop to print odd numbers from 1 to 10
while (counter < 10)
{
counter++;
if (counter % 2 == 0)
{
continue; // Skip the current iteration
}
Console.WriteLine("Odd Number: " + counter);
}
// This will always be executed
Console.WriteLine("Loop finished.");
}
}
}
Output
Odd Number: 1
Odd Number: 3
Odd Number: 5
Odd Number: 7
Odd Number: 9
Loop finished.
Explanation
- Initialization:
int counter = 0;
initializes the counter variable to 0. - Condition Check:
while (counter < 10)
ensures the loop runs as long as the counter is less than 10. - Increment:
counter++;
increments the counter by 1. - Continue Statement:
if (counter % 2 == 0) { continue; }
skips the current iteration when the counter is even. - Loop Body:
Console.WriteLine("Odd Number: " + counter);
prints the current value of the counter if it is odd.
Example: Using continue in a do-while Loop
This example demonstrates the use of the continue
statement in a do-while
loop to skip negative numbers:
using System;
namespace ContinueInDoWhileLoopExample
{
class Program
{
static void Main(string[] args)
{
int[] numbers = { -1, 2, -3, 4, -5, 6, -7, 8, -9, 10 };
int index = 0;
// Using a do-while loop to print positive numbers from the array
do
{
if (numbers[index] < 0)
{
index++;
continue; // Skip the current iteration
}
Console.WriteLine("Positive Number: " + numbers[index]);
index++;
} while (index < numbers.Length);
// This will always be executed
Console.WriteLine("Loop finished.");
}
}
}
Output
Positive Number: 2
Positive Number: 4
Positive Number: 6
Positive Number: 8
Positive Number: 10
Loop finished.
Explanation
- Initialization:
int[] numbers = { -1, 2, -3, 4, -5, 6, -7, 8, -9, 10 };
initializes an array of numbers.int index = 0;
initializes the index variable. - Condition Check:
do { ... } while (index < numbers.Length);
ensures the loop runs as long as the index is less than the length of the array. - Continue Statement:
if (numbers[index] < 0) { index++; continue; }
skips the current iteration when the number at the current index is negative. - Loop Body:
Console.WriteLine("Positive Number: " + numbers[index]);
prints the current value of the array element if it is positive.index++;
increments the index by 1.
Conclusion
The continue
statement is used for controlling the flow of loops in C#. It allows you to skip the remaining code in the current iteration and proceed with the next iteration based on a specific condition. By understanding and using the continue
statement, you can handle various loop scenarios efficiently and write cleaner, more readable code in your C# programs.