#986 – Using goto to Jump to a Label

You can use the goto statement within a block of code to explicitly jump to another location in the code using a label.

            Dog d = new Dog("Bob", 5);

        DoTraining:
            // Train my dog
            d.Train();
            if (d.NumMinutesCanSit < 5)
                goto DoTraining;

            Console.WriteLine("My dog is trained!");

While you can use a goto statement to jump to a label, it’s almost always a bad idea to use goto in this way.  You can always use structured programming techniques, like the while statement, rather than goto.  Code containing goto statements is typically harder to understand than functionally equivalent code written using structured programming constructs.

Here’s the above block of code, re-written to use a while loop.

            do
                d.Train();
            while (d.NumMinutesCanSit < 5);
Advertisement

#177 – Using goto in a switch Statement

You can use a goto statement within a switch statement to jump to another case clause.  This allows you to execute the contents of more than one case clause for a particular value of the switch expression.

            switch (day)
            {
                case DayOfWeek.Sunday:
                    Console.WriteLine("Go to church");
                    goto case DayOfWeek.Saturday;
                case DayOfWeek.Monday:
                    Console.WriteLine("Read Ulysses");
                    break;
                case DayOfWeek.Tuesday:
                    Console.WriteLine("Call Dad");
                    goto case DayOfWeek.Saturday;
                case DayOfWeek.Wednesday:
                    Console.WriteLine("Do laundry");
                    goto case DayOfWeek.Thursday;
                case DayOfWeek.Thursday:
                    Console.WriteLine("Watch a movie");
                    break;
                case DayOfWeek.Friday:
                    Console.WriteLine("Take out the trash");
                    goto case DayOfWeek.Tuesday;
                case DayOfWeek.Saturday:
                    Console.WriteLine("Relax");
                    break;
            }

This results in the following output:

  • Sunday: Go to church; Relax
  • Monday: Read Ulysses
  • Tuesday: Call Dad; Relax
  • Wednesday: Do laundry; Watch a movie
  • Thursday: Watch a movie
  • Friday: Take out the trash; Call Dad; Relax
  • Saturday: Relax

Think carefully before using goto in a switch statement.  There are often more clear ways to implement the same logic.

#160 – A while Loop Can Exit on break, goto, return or throw Statements

In addition to completing normally, when the loop expression evaluates to false, a while loop can exit early.  A loop will exit early if it encounters one of the following statements: break, goto, return or throw.

A break statement causes an early exit of the loop and execution to continue with the first statement after the loop.

            uint numTimes = 0;
            while (numTimes < 100)
            {
                WriteOnBlackboard("I will not eat paste");
                if (ChalkBroke())
                    break;     // Stop writing
                numTimes++;
            }

The goto statement can transfer control out of a while loop, to a labeled statement.  Its use is rare, since there are generally more elegant ways to transfer control.

The return statement can transfer control out of a loop and return control to the calling method.

The throw statement transfers control out of a loop and throws an exception, indicating that an error condition occurred.