Use of Break satement in C#


in the C# language we use break Statement for exit from loop without complete execution of loops.we can use Break statement with all loop statements that use in C# like as for loop,while loop,do while loop,for each loop and switch statment.

Use of Break Statement in C#


In C# and another programming languages,loops are use for perform repeatedly set of operations.The break statement use in C# for break loop execution.

The break statement used for exit from loop without complete execution of for loops.We can use Break Statement with for loop,While loop ,do while loop,for each loop and switch statement.Break statement must be enclosed in the loop blocks otherwise error will be generated.
In this code i will show you working of Break Statement in C#.here we will use break statement with for loop

using System;
using System.Collections.Generic.
using System.Text;

namespace Csharpbreak

{

class Program

{

static void Main(string[] args)

{

for(int x=0; x<4;x++)
{
Console.WriteLine(x);
if(x==2)
break;
}
}
}
}

IN THIS CODE WE SET BREAK STATEMENT WHEN FOR LOOP RUN TWO TIMES AND X VALUE INCREMENT 2.
OUTPUT WILL BE LIKE AS GIVEN BELLOW:
0
1
2
Press any key to conitnue...


Comments

Author: ketan Italiya29 Aug 2013 Member Level: Gold   Points : 4

The break statement in C# has following two usage:
1. When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop.
2. It can be used to terminate a case in the switch statement.
If you are using nested loops ( ie. one loop inside another loop), the break statement will stop the execution of the innermost loop and start executing the next line of code after the block.
Syntax:
The syntax for a break statement in C# is as follows:
break;

Author: sakshi15 Sep 2013 Member Level: Bronze   Points : 0

Break statement can be used in switch and loops as well.it is used to skip the loop and jump to statement following the loop.

Author: Phagu Mahato15 Sep 2013 Member Level: Gold   Points : 3

[1] use of break statement in foreloop
using System;
namespace ExampleBreakStatement
{
class MyProgram
{
static void Main(string[] args)
{
string[] arrayString = new string[] { "Mohan", "Ajay", "Hari", "Fiza" };

foreach (string str in arrayString)
{

if (str == "hari")
break;
else
Console.WriteLine(str);

}
Console.WriteLine();
}
}
}

Author: Phagu Mahato23 Oct 2013 Member Level: Gold   Points : 1

The break statement in C# has following two usage:

When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop.

It can be used to terminate a case in the switch statement.

If you are using nested loops (i.e., one loop inside another loop), the break statement will stop the execution of the innermost loop and start executing the next line of code after the block.

he C# reference explains that the compiler expands a using block into a try/finally block.

This means that a using block like this one

using (create instance)
{
//use instance
}

is turned into this:

{
//create instance
try
{
// use instance
}
finally
{
if (instance != null)
((IDisposable)instance).Dispose();
}
}

Example:



using System;

namespace break_statement
{
class Program
{
static void Main(string args)
{
int i = 0;

while (i greterthen 100)
{
Console.WriteLine(i);
if (i == 20)
{
Console.WriteLine("breaking the current segment...");
break;
}
i++;
}
Console.ReadLine();
}
}
}

Guest Author: saiprasad23 Oct 2013

The break statement terminates the closest enclosing loop or switch statement in which it appears. Control is passed to the statement that follows the terminated statement, if any. This statement takes the following form:

break;



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: