The continue statement in C# is used to skip the current iteration of a loop and move to the next iteration.
It’s useful when you want to bypass certain steps of the loop based on a condition without breaking out of the loop entirely.
The continue statement can be used in any looping construct, including for, while, do-while, and foreach loops.
In this tutorial, we’ll explore:
1. Basic Usage of continue in a for Loop
The for loop is one of the most common places to use the continue statement, allowing you to skip certain iterations based on a condition.
using System; class Program { static void Main() { for (int i = 1; i <= 10; i++) { if (i % 2 == 0) { continue; // Skip even numbers } Console.WriteLine("Odd number: " + i); } } }
Output:
Odd number: 1 Odd number: 3 Odd number: 5 Odd number: 7 Odd number: 9
In this example:
- continue is used to skip the even numbers in the loop. When i is even, the continue statement is executed, and the loop moves to the next iteration without printing the number.
2. Using continue in a while Loop
The continue statement can also be used in a while loop to skip the current iteration.
using System; class Program { static void Main() { int i = 1; while (i <= 10) { i++; if (i % 2 == 1) { continue; // Skip odd numbers } Console.WriteLine("Even number: " + i); } } }
Output:
Even number: 2 Even number: 4 Even number: 6 Even number: 8 Even number: 10
In this example:
- continue skips odd numbers by moving to the next iteration of the loop whenever i is odd.
3. Using continue in a do-while Loop
The continue statement works similarly in a do-while loop. A do-while loop ensures that the loop executes at least once before checking the condition.
using System; class Program { static void Main() { int i = 0; do { i++; if (i % 3 == 0) { continue; // Skip multiples of 3 } Console.WriteLine("Not a multiple of 3: " + i); } while (i < 10); } }
Output:
Not a multiple of 3: 1 Not a multiple of 3: 2 Not a multiple of 3: 4 Not a multiple of 3: 5 Not a multiple of 3: 7 Not a multiple of 3: 8 Not a multiple of 3: 10
In this example:
- continue is used to skip numbers that are multiples of 3, so these numbers are not printed.
4. Using continue in a foreach Loop
In a foreach loop, continue can be used to skip certain items based on a condition.
using System; using System.Collections.Generic; class Program { static void Main() { List<string> names = new List<string> { "Alice", "Bob", "Charlie", "David", "Eve" }; foreach (string name in names) { if (name.StartsWith("C")) { continue; // Skip names that start with 'C' } Console.WriteLine("Name: " + name); } } }
Output:
Name: Alice Name: Bob Name: David Name: Eve
In this example:
- continue is used to skip names starting with “C,” so “Charlie” is not printed.
5. Practical Examples of continue in Real Scenarios
Example 1: Filtering Out Invalid Inputs in a Loop
The continue statement can be helpful for skipping invalid or unwanted data in a loop.
using System; class Program { static void Main() { int[] numbers = { 10, -1, 15, -5, 20 }; Console.WriteLine("Positive numbers:"); foreach (int number in numbers) { if (number < 0) { continue; // Skip negative numbers } Console.WriteLine(number); } } }
Output:
Positive numbers: 10 15 20
In this example:
- continue is used to skip negative numbers, filtering out invalid input.
Example 2: Skipping Iterations in a Nested Loop
continue can be used in nested loops to skip specific iterations within an inner loop.
using System; class Program { static void Main() { for (int i = 1; i <= 3; i++) { Console.WriteLine("Outer loop iteration: " + i); for (int j = 1; j <= 5; j++) { if (j == 3) { continue; // Skip the third iteration of the inner loop } Console.WriteLine(" Inner loop iteration: " + j); } } } }
Output:
Outer loop iteration: 1 Inner loop iteration: 1 Inner loop iteration: 2 Inner loop iteration: 4 Inner loop iteration: 5 Outer loop iteration: 2 Inner loop iteration: 1 Inner loop iteration: 2 Inner loop iteration: 4 Inner loop iteration: 5 Outer loop iteration: 3 Inner loop iteration: 1 Inner loop iteration: 2 Inner loop iteration: 4 Inner loop iteration: 5
In this example:
- continue skips the third iteration of the inner loop in each outer loop iteration, so it does not print the value 3 for j.
Example 3: Skipping Odd Numbers in a Range
Using continue in a for loop to process only even numbers.
using System; class Program { static void Main() { Console.WriteLine("Even numbers from 1 to 10:"); for (int i = 1; i <= 10; i++) { if (i % 2 != 0) { continue; // Skip odd numbers } Console.WriteLine(i); } } }
Output:
Even numbers from 1 to 10: 2 4 6 8 10
In this example:
- continue is used to skip odd numbers, so only even numbers are printed.
Summary
In this tutorial, we covered the continue statement in C# and demonstrated its use in different types of loops:
- for loop: Used continue to skip specific numbers based on a condition.
- while loop: Skipped specific values while iterating through a loop.
- do-while loop: Used continue to skip iterations in a do-while loop.
- foreach loop: Demonstrated continue to skip elements in a collection.
- Practical examples: Showed scenarios such as filtering invalid inputs, handling nested loops, and printing specific numbers.
The continue statement is a useful control flow tool for managing loop iterations and efficiently handling conditions within loops.