Jump Statements In C#

In this article I am going to explain three jump statements; they are goto, break and continue.

The jump statements in C# are the statements that allow us to move the program control from one point to another at any particular instance during the execution of the program.

There are five types of jump statements used in C# as follows:

  1. Goto
  2. Break
  3. Continue
  4. Return
  5. Throw

Now, let's discuss each of the jump statements (goto, break and continue) in detail one by one.

1. Goto Statement

The goto statement branches unconditionally from one point to another in the block. In other words the goto statement transfers execution to another label within the statement block.

The goto requires a label to identify the place where the branch is to be made to. A label is any valid identifier and must be followed by a colon. The label is placed immediately before the statement where the control is to be transferred. The goto statement can transfer the control to any place in a program.

There are two forms of goto statements Forward jump and Backward jump.

JumpSt1.jpg

For example we create a program that transfers execution to the "displayInfo" label:

using System;

namespace ConsoleApplication

{

    class Program

    {

     

        static void Main(string[] args)

        {

            Console.WriteLine("Goto  Example Start");

            goto displayInfo;

            Console.WriteLine("This code skipped code");

            displayInfo:

            {

                Console.WriteLine("It is displayInfo block");

            }            

            Console.Read();

        }

    }

}

JumpSt2.jpg

2. Break Statement

It terminates the loop in which it exists. It also changes the flow of execution of a program. After the loop terminates, the program transfers control to the next block of statements following it.

When the loops are nested, the break would exit from the loop containing it; in other words, the break will exit only a single loop iteration.

JumpSt3.jpg

For example we have an iteration from 1 to 10 but we want that when the pointer is incremented to 5 it exits from this and prints only from 1 to 4.
 

using System;

namespace ConsoleApplication

{

    class Program

    {

     

        static void Main(string[] args)

        {

            for (int i = 1; i <= 10; i++)

            {

                if (i == 5)

                    break;

               

               Console.WriteLine("Value of i is {0}", i);

               

            }

                Console.Read();

        }

    }

}

JumpSt4.jpg

3. Continue Statement

It is used to transfer the program control to the beginning of the loop. It continues until a specific condition is satisfied; in other words, the continue statement skips the remaining code in the loop and the pointer is sent to the beginning of the loop. The continue statement tells the complier to skip the following statements and continue with the next iteration.

JumpSt5.jpg

For example we want to run an iteration from 1 to 5 and we don't want to print 3. So when we increment the pointer and it reaches 3 then skip the following code.
 

using System;

namespace ConsoleApplication

{

    class Program

    {     

        static void Main(string[] args)

        {

            for (int i = 1; i <= 5; i++)

            {

                if(i==3)

                    continue;

 

                Console.WriteLine("Value of i is {0}", i);

            }

                Console.Read();

               

               

        }

    }

}

JumpSt6.jpg