C# Jump Statements

Jump statements are the statements that allow you to move the program control from one point to another, at any particular instance during the execution of the program. In C#, there are five types of jump statements, which are as follow:

Types of jump statements

  • goto statement - Transfer the program control directly to a labeled statement. The statement or block of statements that have been given some name or label is known as a labeled statement.

  • break statement - Terminates the loop in which it appears. Therefore, the program control is transferred to the statement that follows the break statement.

  • continue statement - Transfers the program control to the next iteration in which the continue keyword appears.

  • return statement - Terminates the execution of the method in which it appears and returns control to the calling method.

  • throw statement - Specifies the occurrence of an exception during program execution.

The goto Statement

The goto statement is used to transfer the program control to a statement that has the same label as that of the goto statement. The statement having the same label is known as the labeled statement. More than one goto statement can transfer the program control to the same labeled statement. The goto statement can transfer the program control outside a control statement; however, it cannot transfer the program control inside the control statement, such as the while loop, the do...while loop, and the for loop. Generally, the use of goto statements is not recommended as it increases the complexity of the program execution. However, they can be used in switch statements or when you want to transfer the program control outside a nested loop. The following code snippet shows an example of the goto statement:

if(Number1 > Number2)
{
 if(Number1 > Number3)
 {
     goto MyLabel;  //label
 }
}
MyLabel:  //labeled statement
{
    Console.WriteLine("Number1 is greater than Number2");
    ...//statements
}

In the preceding code snippet, you can see that the goto statement (goto MyLabel;) inside the nested if statements transfers the program control to a labeled statement (MyLabel:).

The break statement

The break statement terminates the loop in which it exists. It can also alter the flow of a loop. After exiting the loop, the program control is transferred to the next block of statements following the loop. You can use the break statement to exit from a case in a switch statement or control statements, such as for, foreach, while, or do...while loop. The following code snippet shows the use of the break statement:

{
int number;
for (number = 1; number <=10; number++)
{
    Console.WriteLine(number);
    if (number>=5)
    break;  //break statement
}
Console.WriteLine("Press Enter to quit...");
Console.ReadLine();
}

The continue statements

The continue statement starts a new iteration of the enclosing loop in which it exists. This statement can exist in different kinds of loop statements, such as while, do...while, for, or foreach loop. The continue executes the statements following it when a certain condition is not fulfilled. This statement must be enclosed within a loop; otherwise, a compile-time error occurs. The following code snippet shows how to use the continue statement:

{
 int number;
 for (number = 1; number <= 10; number++)
 {
     if (number<=5)
     continue;
     Console.WriteLine(number);
 }
 Console.WriteLine("Press Enter to quit...");
 Console.ReadLine();
}


Write a comment


Name Email Message