C# Goto

C# goto statement terminate the current execution flow and turn the control to the labelled statement. Actually, all the loop statements or selection statements can be replaced by goto statement. In the history of the software engineering, goto statement was regarded as the worst example when developing a large software. Because it made the code hard to be traced and destroyed whole design structure. For example, goto statement is not allowed to be used in java language.

In C#, it is recommended to be used in switch statement or jump out of a deep nested loops.

Syntax:

switch( n ){
    case 1:
        Statements 1;
        [break|goto case x];
    case 2:
        Statements 2;
        [break|goto case x];
    ......
    case x:
        Statements x;
        break;
    ......
    default:
        Statements;
        break;
}

In some scenarios, you need replace break statement with goto case x statement. Check the example below.

Example 01-37-01

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;

namespace TestGotoInSwitch
{
    class Program
    {
        static void Main(string[] args)
        {
            char result = 'A';

            switch( result ){
                case 'A':
                    Console.WriteLine("You got A.");
                    goto case 'C';
                case 'B':
                    Console.WriteLine("You got B.");
                    goto case 'C';
                case 'C':
                    Console.WriteLine("You passed the exam.");
                    break;
                default:
                    Console.WriteLine("You did not pass the exam.");
                    break;
            }
            Console.Read();
        }
    }
}

Output

You got A.
You passed the exam.
  • Line 11: Start Switch statement to evaluate the value of result.
  • Line 14,17: Goto statements. If the result is case 'A' or case 'B', we will go to case 'C' eventually.
  • Line 12-13: In this example, we'll go here to output "You got A.".
  • Line 14: goto case 'C'.
  • Line 18-19: Output "You passed the exam.".
  • Line 20: break statement makes the control goes to line 25.

Syntax:

    Loop 1 Start{
        Loop 2 Start{
        ......
        goto Label;
        ......
        }
    }
Label:
    Statement n;

The above syntax is as is. The goto statement jumps out of the deep loops and transfer the control to the statement n just following the "Label". You can see the example below.

Example 01-37-02

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;

namespace gotoNested
{
    class Program
    {
        static void Main(string[] args)
        {
            const int Max = 10;

            for(int i=1; i<Max; i++){
                for(int j=1; j<Max; j++){
                    if ( i==5 && j == 5 ) goto outside;
                    Console.Write("{0}x{1}={2} ", i, j, i*j);
                }
                Console.WriteLine();
            }
outside:
            Console.WriteLine("Stop!");
            Console.Read();
        }
    }
}

Output

1x1=1 1x2=2 1x3=3 1x4=4 1x5=5 1x6=6 1x7=7 1x8=8 1x9=9 
2x1=2 2x2=4 2x3=6 2x4=8 2x5=10 2x6=12 2x7=14 2x8=16 2x9=18 
3x1=3 3x2=6 3x3=9 3x4=12 3x5=15 3x6=18 3x7=21 3x8=24 3x9=27 
4x1=4 4x2=8 4x3=12 4x4=16 4x5=20 4x6=24 4x7=28 4x8=32 4x9=36 
5x1=5 5x2=10 5x3=15 5x4=20 Stop!
  • This example is to output 1-9 times table but stopped when starting 5x5.
  • Line 11-12: 2 for statements start.
  • Line 13: If statement. it will run "goto statement" when i=5 and j=5. The goto statement will pass the control to the statement following the label "outside". In this example, it is line 19.
  • Line 14: Output times table.
  • Line 18: The label "outside".
  • Line 19: Output "Stop!" when going to this statement.
Note Note
When using goto label statement, you will get compile time error if the label doesn't exist in the context.

Note Note
It is not allowed to "goto" inside a loop statement from outside.