Search code examples
c#switch-statementgoto

The goto command doesn't work


When I'm doing the code without the goto command it works, but when I add the :Start it get an 8 error.

Here is the relevant code:

        :Start
        Console.Write("Do you want the yes or no?");
        string what = Console.ReadLine();
        switch (what)
        {
            case "yes":
                Console.WriteLine("You choose yes");
                break;
            case "no":
                Console.WriteLine("You choose no");
                break;
            default:
                Console.WriteLine("{0},is not a word",what);
                goto Start;
         }

Solution

  • The correct syntax is Start:. But, instead of goto, you should set this up in a loop:

    bool invalid = true;
    while (invalid)
    {
        Console.Write("Do you want the yes or no?");
        string what = Console.ReadLine();
        switch (what)
        {
            case "yes":
                Console.WriteLine("You choose yes");
                invalid = false;
                break;
            case "no":
                Console.WriteLine("You choose no");
                invalid = false;
                break;
            default:
                Console.WriteLine("{0},is not a word",what);
         }
    }