Saturday, 25 January 2014

Break statement in C#

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 (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.

Syntax:

The syntax for a break statement in C# is as follows:
break;

Flow Diagram:

c# break statement

Example:

using System;  
namespace break_statement
{  
class Program
{
static void Main(string[] args)       
{         
int i = 0;           
while (i < 100)          
{            
Console.WriteLine(i);            
if (i == 20)             
{               
Console.WriteLine("breaking the current segment...");               break;             
}            
i++;          
}           
Console.ReadLine();      
 }   
 }
}
When the above code is compiled and executed, it produces the following result:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
breaking the current segment

No comments:

Post a Comment

Paging in html table using javascript

I demonstrated how to use HTML tables on the client for a very simple client-side paging solution. I have heard from several people who p...