codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

C# Programming

12 / 60 topics
6Control Structures in C#7If-Else Statements8Switch Case Statements9For Loops10While Loops11Do-While Loops12Break and Continue Statements
Tutorials/C# Programming/Break and Continue Statements
🔷C# Programming

Break and Continue Statements

Updated 2026-05-15
10 min read

Break and Continue Statements

Introduction

In C#, control flow statements are essential for managing the execution of your code. Two powerful statements that help in controlling loops are break and continue. These statements allow you to exit a loop prematurely or skip certain iterations, respectively. Understanding how to use these statements effectively can greatly enhance the efficiency and readability of your programs.

Concept

Break Statement

The break statement is used to immediately terminate the execution of a loop (for, foreach, while, do-while) or a switch block. When the break statement is encountered, the control flow exits the loop or switch and continues with the next statement following the loop or switch.

Continue Statement

The continue statement is used to skip the current iteration of a loop and proceed directly to the next iteration. Unlike the break statement, which exits the entire loop, continue only skips the current cycle and continues with the next one.

Examples

Let's explore some practical examples to understand how break and continue work in C#.

Using Break Statement

Suppose you want to find a specific number in an array and stop searching once it is found. You can use the break statement to exit the loop as soon as the number is located.

csharp
1int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
2int target = 5;
3
4foreach (int num in numbers)
5{
6 if (num == target)
7 {
8 Console.WriteLine($"Number {target} found!");
9 break;
10 }
11}
Output
Number 5 found!

In this example, the loop iterates through each number in the array. When it finds the number 5, it prints a message and exits the loop using the break statement.

Using Continue Statement

Imagine you want to print all numbers from 1 to 10 except for even numbers. You can use the continue statement to skip printing even numbers.

csharp
1for (int i = 1; i <= 10; i++)
2{
3 if (i % 2 == 0)
4 {
5 continue;
6 }
7 Console.WriteLine(i);
8}
Output
1
3
5
7
9

Here, the loop iterates from 1 to 10. When it encounters an even number (i.e., i % 2 == 0), it skips the current iteration using the continue statement and proceeds to the next one.

Nested Loops with Break

The break statement can also be used in nested loops to exit from a specific loop level. By default, break exits the innermost loop. To break out of an outer loop, you can use labels.

csharp
1outerLoop:
2for (int i = 1; i <= 3; i++)
3{
4 for (int j = 1; j <= 3; j++)
5 {
6 if (i == 2 && j == 2)
7 {
8 break outerLoop;
9 }
10 Console.WriteLine($"({i}, {j})");
11 }
12}
Output
(1, 1)
(1, 2)
(1, 3)
(2, 1)

In this example, the break outerLoop; statement exits both the inner and outer loops when i is 2 and j is 2.

What's Next?

Now that you have a good understanding of how to use break and continue statements in C#, it's time to explore more advanced control flow techniques. In the next section, we will delve into methods in C#, including defining methods, passing parameters, and returning values. This knowledge will further enhance your ability to write structured and reusable code.

Stay tuned for more tutorials on codingstuff.io!


PreviousDo-While LoopsNext Methods in C#

Recommended Gear

Do-While LoopsMethods in C#