The C# jump statements are break
, continue
, goto
, return
, and throw
.
The C# break statement ends the execution of the body of an iteration or switch statement:
int x = 0;
while (true) {
if (x++ > 5)
break ; // break from the loop
}
// execution continues here after break
Syntax of C# Break Statement:
jump-statement;
break;
C# Break Statement Flow Chart:
Let's see a simple example of C# break statement which is used inside the loop:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
break;
}
Console.WriteLine(i);
}
//Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
}
}
Output:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
int x = 0;
while (true)
{
Console.WriteLine("before break:" + x);
if (x++ > 5)
break; // break from the loop
Console.WriteLine("after break:" + x);
}
Console.ReadLine();
}
}
}
Output:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
/* local variable definition */
int a = 10;
/* while loop execution */
while (a < 20)
{
Console.WriteLine("value of a: {0}", a);
a++;
if (a > 15)
{
/* terminate the loop using break statement */
break;
}
}
Console.ReadLine();
}
}
}
Output:
The C# break statement breaks inner loop only if you use break statement inside the inner loop.
Let's see the example code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
if (i == 2 && j == 2)
{
break;
}
Console.WriteLine(i + " " + j);
}
}
Console.ReadLine();
}
}
}
Output:
When the above code is compiled and executed, it produces the following result:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 4; i++)
{
if (i == 3)
break;
Console.WriteLine("i value: {0}", i);
}
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
}
}
Output:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
int i = 1;
while (i < 4)
{
Console.WriteLine("i value: {0}", i);
i++;
if (i == 2)
break;
}
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
}
}
Output:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
int i = 1;
do
{
Console.WriteLine("i value: {0}", i);
i++;
if (i == 2)
break;
} while (i < 4);
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
}
}
Output: