Illustration of Jump Statements

Illustration of Jump Statements (continue, goto) supported in C#

In this article you are going to discuss two important jump statements namely continue statement and goto statement.

Continue Statement:

Unlike break statement, continue statement is used to skip remaining statements in the current iteration and continue with the next iteration. Consider the following example:

class sampleClass {

public static void Main(){

Console.Write(“Even Numbers less than 11 are:”);

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

if(index%2 !=0) {

continue;

}

Console.Write(index+” “);

}

}

}

Output of this code will be:

Even Numbers less than 11 are 2 4 6 8 10

As shown in the example above when the continue statement is encountered the console statement will not be printed and the control goes to next iteration. Remember that if break statement is used in place of continue then the next iteration will not be performed. Instead control will come out of the entire loop.

Goto Statement:

Goto statement is used to jump from the current position to the line of code where the label specified along with the goto statement is residing. Goto statement is mainly used in the following scenarios:

* In the switch statement to execute certain cases in addition to the specific case which matches switch condition

* It is used to come out of a loop that is nested to multiple levels

Each of these cases is discussed with examples in this article.

Using goto Statement in Switch:

You have a scenario in which User can enter a value from 10 to 13. You have to print the number back to the User along with the information stating whether the number is even or odd. You have to perform this logic using switch statement. How will you do it? Here is the code sample for it:

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.Write(“You Entered 10.”);

goto case 14;

case 11:

Console.Write(“You Entered 11.”);

goto case 15;

case 12:

Console.Write(“You Entered 12.”);

goto case 14;

case 13:

Console.Write(“You Entered 13.”);

goto case 15;

case 14:

Console.Write(“It is an Even Number.”);

break;

case 15:

Console.Write(“It is an Odd Number.”);

break;

default:

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

}

}

}

Assume that the User enters number 12 then the output will be:

You Entered 12.It is an Even Number.

If the User entered the number 11 then the output will be:

You Entered 11. It is an Odd Number.

In this example when User enters a specific number, you are directing the User to the specific case of switch statement which prints the number. In addition, you are using goto along with the case statement which prints whether the number is odd or even. Since the numbers are very specific and you know if it is even or odd, you directly have two case statements printing that it is an even/odd number and you are redirecting from the earlier case statement using goto statement.

Using goto Statement in Nested For Loop:

To understand how goto statement is used in loop, get a set of numbers from the User. Ask the User to specify the number for which he has to perform the search. Search that specified number and if it exists, print the array index position in which it exists and end the program. How do you do it? Here is the sample code for it:

class sampleClass {

public static void Main() {

int[,] sample= new int[5,5];

string temp;

for(int index1 = 0; index1<5; index1++) {

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

Console.WriteLine(“Enter Integer Value:”);

temp = Console.ReadLine();

sample[index1,index2] = Int32.parse(temp);

}

}

Console.WriteLine(“Enter the Value Whose Position has to be returned:”);

temp = Console.ReadLine();

int data = Int32.parse(temp);

for(int index1 = 0; index1<5; index1++) {

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

if(sample[index1,index2] == data) {

Console.WriteLine(“Number Found in Array[{0}]{[1]}”,

index1, index2);

goto Success;

}

}

}

Success: Console.WriteLine(“Hope to Meet You Again!”);

}

}

In this example, you might have used break statement. But that will come out of the inner loop only. Since you want to come out of both the loops, goto statement has to be used.

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!