Illustration of Break Statement in C#

Illustration of Break Statement in C#

Assume that your program includes 1000 lines of code. In general, these lines of code will be executed in sequence. What if you want to avoid this sequence of execution and branch to a different set of code? That is achieved using jump statements of C#. C# uses 5 different keywords in the jump statements. They are break, continue, goto, return and yield. This article will analyze about how this break keyword influences the jump statements.

The break statement can be used in the following cases:

* To jump out of the innermost loop. The loop statement can be either do or for or while.

* To jump out of the switch statement.

Both these cases are discussed below with examples.

Usage of Break Statement inside for Loop:

Consider the following example:

public class sampleClass {

public static void Main(){

for(int index = 1; index<= 10; index++) {

Console.Write(index+” “);

}

}

}

Output of this code will be: 1 2 3 4 5 6 7 8 9 10

In this example, you have for loop which iterates 10 times and displays the index value. What if you want to exit from the loop when the index value is an even number? You can do it using break statement. Alter the above mentioned for loop as shown below:

for(int index=1; index<=10; index++) {

if(index%2 == 0) {

break;

}

Console.Write(index);

}

Output of this code will be: 1

This example has a single for loop. What if you have a nested for loop that uses break statement inside the innermost for loop? When the break statement is encountered, the control comes out of the innermost for loop. However iteration of the outer for loops will continue. Here is an example to demonstrate this case:

public class sampleClass {

public static void Main(){

for(int index1 = 1; index1 <= 3; index1++) {

Console.WriteLine(‘Outer Loop: “+ index1);

for(int index2 = 1; index2 <= 5; index2++) {

if(index2 == index1) {

break;

}

Console.Write(“Inner Loop Value: ”+ index2);

}

}

}

}

Output of this code will be:

Outer Loop: 1

Outer Loop: 2

Inner Loop Value: 1

Outer Loop: 3

Inner Loop Value: 1

Inner Loop Value: 2

Usage of Break Statement inside Switch:

Consider the following example illustrating usage of switch statement:

class sampleClass {

public static void Main(){

Console.Write(“Enter Any Number In the Range 10-13:”);

string inputStr = Console.ReadLine();

int input = Int32.Parse(inputStr);

switch(input) {

case 10:

Console.WriteLine(“You Entered 10”);

case 11:

Console.WriteLine(“You Entered 11”);

case 12:

Console.WriteLine(“You Entered 12”);

case 13:

Console.WriteLine(“You Entered 13”);

default:

Console.WriteLine(“You Entered An Incorrect Value”);

}

}

}

What do you think is the output of this code if the User has entered the value 12? Output is:

You Entered 12

You Entered 13

You Entered An Incorrect Value

Is this output correct? NO. Where is the problem? When User entered 12, case 12 of switch statement gets executed and the console statement gets printed. After that your job is done and you have to come out of the switch statement. But you didn't include any statement inside case 12 to bring the control out of switch. You have to include break statement as the last line of each case to ensure that rest all cases in the sequence are not executed. The problem of the above example is rectified when you change the switch block as shown below:

switch(input) {

case 10:

Console.WriteLine(“You Entered 10”);

break;

case 11:

Console.WriteLine(“You Entered 11”);

break;

case 12:

Console.WriteLine(“You Entered 12”);

break;

case 13:

Console.WriteLine(“You Entered 13”);

break;

default:

Console.WriteLine(“You Entered An Incorrect Value”);

}

Since the default block of the switch is mentioned at the last, break statement need not be included in it. Now if the User enters the value to be 12, the output will be:

You Entered 12

References:

http://msdn.microsoft.com

www.dotnetperls.com

www.csharpfriends.com

Please be aware that the free essay that you were just reading was not written by us. This essay, and all of the others available to view on the website, were provided to us by students in exchange for services that we offer. This relationship helps our students to get an even better deal while also contributing to the biggest free essay resource in the UK!