In C#, Jump statements are used to transfer control from one point to another point in the program due to some specified code while executing the program. There are five keywords in the Jump Statements:

  • break
  • continue
  • goto
  • return
  • throw
     

break statement

The Break statement is used to terminate the loop or statement it is in. Control is then passed to the statement following the break statement, if available. If a Break statement is present within a nested loop, only the loop containing the break statement is terminated. 

Example: 

// C# program to illustrate the
// use of break statement
usingSystem;
class CodeConfigs
{    
// Main Method   
 staticpublicvoidMain()
   {     
   // CodeConfig.in is printed only 2 times   
     // because of break statement    
    for(inti = 1; i < 4; i++)      
   {        
    if(i == 3)    
            break;    
        Console.WriteLine("CodeConfig.in");      
  }   
 }}

Output: CodeConfig.in CodeConfig.in

continue statement

This statement is used to skip the execution part of a loop under certain conditions. Control is then passed to the beginning of the loop. Basically, the next statement is skipped and you move on to the next iteration of the loop.
 Example:

// C# program to illustrate the
// use of continue statement
usingSystem;
class CodeConfig
{   
 // Main Method    public static void Main()
    {               
 // This will skip 4 to print      
  for(inti = 1; i <= 10; i++) {   
         // if the value of i becomes 4 then  
           // it will skip 4 and send the   
         // transfer to the for loop and    
         // continue with 5   
          if(i == 4)    
            continue;   
        Console.WriteLine(i);    
    }  
  }}

Output:1 2 3 5 6 7 8 9 10

goto statement

This statement is used to transfer control to the labeled statement in the program. The label is the valid identifier and placed just before the statement from where the control is transferred. 

C# | Jump Statements

Example:


// Below is C# program to illustrate the
// use of goto statement
using System;
 
class CodeConfigs
{
     // Main Method
    static public void Main()
    {
        int number = 20;
        switch (number) {
 
        case 5:
            Console.WriteLine("case 5");
            break;
        case 10:
            Console.WriteLine("case 10");
            break;
        case 20:
            Console.WriteLine("case 20");
 
            // goto statement transfer
            // the control to case 5
            goto case 5;
 
        default:
            Console.WriteLine("No match found");
             
        }
    }
}

Output: case 20 case 5

return statement

This statement terminates the execution of the method and returns the control to the calling method. It returns an optional value. If the type of method is void, then the return statement can be excluded.
Example: 

// Below is C# program to illustrate the
// use of return statement
using System;
 
class CodeConfigs{
 
    // creating simple addition function
    static int Addition(int a)
    {
 
        // add two value and
        // return the result of addition
        int add = a + a;
        
        // using return statement
        return add;
    }
 
    // Main Method
    static public void Main()
    {
        int number = 2;
 
        // calling addition function
        int result = Addition(number);
        Console.WriteLine("The addition is {0}", result);
    }
}

Output: The addition is 4

throw statement

This is used to create an object of any valid exception class with the help of new keyword manually. The valid exception must be derived from the Exception class.
Example:

/ C# Program to illustrate the use
// of throw keyword
using System;
 
class CodeConfigs{
     
     // taking null in the string
     static string sub = null;
         
    // method to display subject name   
    static void displaysubject(string sub1)
    {
        if (sub1  == null)
            throw new NullReferenceException("Exception Message");
             
    }
 
// Main Method   
static void Main(string[] args)
{
     
    // using try catch block to
    // handle the Exception
    try
    {
         
        // calling the static method
        displaysubject(sub);
    }
     
    catch(Exception exp)
    {
        Console.WriteLine(exp.Message );
    }                    
}
 
}

Output: Exception Message

In this article we tried to explain C# | Jump Statements with Examples. I hope you enjoyed reading this article. For more information you can check out Microsoft learn.

For more information you can visit our C# Section for more such articles

Explore More

Working With File Class In C#

In C#, ‘File’ class provides many static methods for moving, copying, and deleting files. These Static methods involve moving a file and copying and deleting a file also. Let’s check

How to Read a Text File in C#

In C# ‘File’ class provides static methods to read given text file data. The File.ReadAllText() method opens a text file, reads all the text present in the file into a

C# | How to use Interface references

In C#, you’re permitted to make a reference variable of an interface type or in other words, you’re permitted to form an interface reference variable. Such kind of variable can