Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to control for loop using break and continue statements in C#?
The break and continue statements provide powerful control flow mechanisms within for loops in C#. The break statement terminates the loop entirely, while the continue statement skips the current iteration and moves to the next one.
Syntax
Following is the syntax for using break statement in a for loop −
for (initialization; condition; increment) {
if (someCondition) {
break; // exits the loop completely
}
// other statements
}
Following is the syntax for using continue statement in a for loop −
for (initialization; condition; increment) {
if (someCondition) {
continue; // skips to next iteration
}
// other statements (skipped if continue executes)
}
Using 'break' Statement
The break statement terminates the loop immediately when a specific condition is met. Here's an example that finds numbers and stops when it encounters a specific value −
using System;
class Program {
public static void Main() {
int[] numbers = {1, 3, 7, 9, 12, 15, 18};
Console.WriteLine("Looking for first number greater than 10:");
for (int i = 0; i < numbers.Length; i++) {
Console.WriteLine("Checking number: " + numbers[i]);
if (numbers[i] > 10) {
Console.WriteLine("Found: " + numbers[i]);
break; // exits the loop
}
}
Console.WriteLine("Loop ended.");
}
}
The output of the above code is −
Looking for first number greater than 10: Checking number: 1 Checking number: 3 Checking number: 7 Checking number: 9 Checking number: 12 Found: 12 Loop ended.
Using 'continue' Statement
The continue statement skips the current iteration and moves directly to the next iteration. It's useful when you want to skip certain values but continue processing others −
using System;
class Program {
public 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);
}
Console.WriteLine("Loop completed.");
}
}
The output of the above code is −
Even numbers from 1 to 10: 2 4 6 8 10 Loop completed.
Comparison of break vs continue
| break Statement | continue Statement |
|---|---|
| Terminates the loop completely | Skips current iteration only |
| Control moves to the statement after the loop | Control moves to the loop's increment/condition |
| Used to exit when a condition is met | Used to skip unwanted iterations |
| No further iterations are executed | Remaining iterations continue normally |
Combined Example
Here's an example demonstrating both break and continue in action −
using System;
class Program {
public static void Main() {
int sum = 0;
int count = 0;
Console.WriteLine("Processing numbers 1 to 20:");
Console.WriteLine("Skipping multiples of 3, stopping at first multiple of 17");
for (int i = 1; i <= 20; i++) {
if (i % 17 == 0) {
Console.WriteLine("Found multiple of 17: " + i + ", breaking loop");
break;
}
if (i % 3 == 0) {
Console.WriteLine("Skipping " + i + " (multiple of 3)");
continue;
}
sum += i;
count++;
Console.WriteLine("Added " + i + " to sum");
}
Console.WriteLine("Final sum: " + sum + ", Count: " + count);
}
}
The output of the above code is −
Processing numbers 1 to 20: Skipping multiples of 3, stopping at first multiple of 17 Added 1 to sum Added 2 to sum Skipping 3 (multiple of 3) Added 4 to sum Added 5 to sum Skipping 6 (multiple of 3) Added 7 to sum Added 8 to sum Skipping 9 (multiple of 3) Added 10 to sum Added 11 to sum Skipping 12 (multiple of 3) Added 13 to sum Added 14 to sum Skipping 15 (multiple of 3) Added 16 to sum Found multiple of 17: 17, breaking loop Final sum: 91, Count: 10
Conclusion
The break and continue statements provide essential loop control in C#. Use break to exit a loop when a specific condition is met, and continue to skip unwanted iterations while allowing the loop to proceed with remaining iterations.
