Jumping of execution is another essential concept of programming, and the different jump statements help us do so. These statements help in transferring control from one point to the other. Positioning the execution based on a certain requirement is another concept that helps in jumping from a particular logic to others within a program. In this chapter, you will learn about C# jump statements.



Jump Statements in C#

C# provides programmers with five different types of jump statements. These are:
  1. goto
  2. break
  3. continue
  4. return
  5. throw

goto

This statement is used to control the transfer of execution to a labeled statement within a program. The label has to be a valid identifier of C# before or after any program statement where the control needs to be transferred.

Example:

using System;
namespace ConsoleApplication
{
class ProgEg
{ 
    static void Main(string[] args)
    {
        Console.WriteLine(" Goto Starts ");
        goto g;
        Console.WriteLine(" This line gets skipped ");
        g:
        {
            Console.WriteLine(" This section will be displayed ");
        }            
        Console.Read();
    }
}
}

Output:

Goto Starts 
This section will be displayed

break

This statement is implemented to expire any iteration or statement with which it is associated. The program execution will get passed to the next statement out of the iteration as soon as the break is encountered.

Example:

using System;   
class ProgEg {   
  static public void Main()     { 
    for (int g = 1; g < 6; g++)          { 
        if (g == 4) 
           break;   
        Console.WriteLine(" Hello world "+g); 
    } 
} 
}

Output:

Hello world 1
Hello world 2
Hello world 3

continue

This jump statement will leave out some part of execution statements from within the loop on a specific condition and put the control of program execution to the beginning of the loop.

Example:

using System; 
class ProgEg {  
public static void Main() 
{ 
    for (int i = 1; i <= 10; i++) { 
            continue; 
        Console.WriteLine(i); 
    } 
} 
}

return

This jump statement is used to terminate any execution flow in any method. It returns control to the calling method. The value returned by it is optional, and when the return type is void, the return statement can be debarred.

Example:

using System; 
class ProgEg { 
static int Add(int aa) 
{
    int a = aa + aa; 
    return a; 
} 
static public void Main() 
{ 
    int numb = 6; 
    int res = Add(numb); 
    Console.WriteLine(" Addition is: {0} : ", res); 
} 
}

Output:

Addition is: 12

throw

This is a concept of exception handling in C#, which will be covered in the later chapters. But since it also jumps program flows and executions by creating an object of valid exception class using the new

Example:

using System; 
class ProgEg
 { 
    static string s = null;  
    static void disp(string s1) 
    { 
        if (s1  == null) 
            throw new NullReferenceException("Exception Found.");              
    }    
static void Main(string[] args) 
{  
    try
    { 
        disp(s); 
    }  
    catch(Exception exp) 
    { 
        Console.WriteLine( exp.Message ); 
    }                      
} 
}

Output:

Exception Found.

The exception thrown by the throw statement will be taken care of by the catch statement, which jumps the execution flow directly from throw to catch.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram