C# Jump Statements

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

1. break

2. continue

3. goto

4. return

5. throw

 

1. break Statement: The break statement is used to terminate the loop or statement it encountered. The control is then passed onto the statements after the break statement.

 

using system;

class Program{
    static public void main()
    {
        for (int i = 1; i<5; i++)
        {
            if (i == 4){
                break;
            }
            Console.WriteLine("Hello Progmr");
        }
    }
}

2. continue Statement: the continue statement is used to skip the execution of loop on condition on which it is encountered and executes the statements after it or continue to later execution of the loop.

 

using system;

class Program{
    static public void main()
    {
        for (int i = 1; i<5; i++)
        {
            if (i == 4){
                continue;
            }
            Console.WriteLine("Hello Progmr");
        }
    }
}

3. goto Statement: The goto statement is used to transfer the control to the labeled statement.

using System;

class Program{
    static public void Main()
    {
        int num = 20;
        switch(num){
            case 15:
            Console.WriteLine("This is case 15.");
            case 20:
            goto case 15;
        }
    }
}

4. return Statement: The return statement terminates the execution of the method and the control is returned to the calling method. It return void if the method is of void, then the return statement ca be excluded.

 

using System;

class Program{
    static int add(int a){
        int b = a + a;
        return b;
    }
    static public void Main(){
        int num = 1;
        int result = add(num);
        Console.WriteLine("The Addition is ",result);
    }
}

5. throw Statement: It is used to create the object of valid exception class with the help of new keyword.

using System;

class Program{
    static string mes = null;
         
    static void showString(string sub1)
    {
        if (sub1  == null)
            throw new NullReferenceException("Exception Caught Message");
             
    }
    static void Main(string[] args){
        try{
           showString(mes);
        }
        catch(Exception e){
            Console.WriteLine(e.Message);
        }
    }
}