goto (Referencia de C#)

Actualización: noviembre 2007

La instrucción goto transfiere el control del programa directamente a una instrucción identificada por una etiqueta.

Un uso habitual de goto consiste en transferir el control a una etiqueta switch-case específica o a la etiqueta predeterminada de una instrucción switch.

La instrucción goto también es útil para salir de bucles de varios niveles de anidamiento.

Ejemplo

El ejemplo siguiente muestra cómo utilizar goto en una instrucción switch.

class SwitchTest
{
    static void Main()
    {
        Console.WriteLine("Coffee sizes: 1=Small 2=Medium 3=Large");
        Console.Write("Please enter your selection: ");
        string s = Console.ReadLine();
        int n = int.Parse(s);
        int cost = 0;
        switch (n)
        {
            case 1:
                cost += 25;
                break;
            case 2:
                cost += 25;
                goto case 1;
            case 3:
                cost += 50;
                goto case 1;
            default:
                Console.WriteLine("Invalid selection.");
                break;
        }
        if (cost != 0)
        {
            Console.WriteLine("Please insert {0} cents.", cost);
        }
        Console.WriteLine("Thank you for your business.");
    }
}
/*
Sample Input:  2

Sample Output:
Coffee sizes: 1=Small 2=Medium 3=Large
Please enter your selection: 2
Please insert 50 cents.
Thank you for your business.
*/

El siguiente ejemplo muestra el uso de goto para salir de un conjunto de bucles anidados.

public class GotoTest1
{
    static void Main()
    {
        int x = 200, y = 4;
        int count = 0;
        string[,] array = new string[x, y];

        // Initialize the array:
        for (int i = 0; i < x; i++)

            for (int j = 0; j < y; j++)
                array[i, j] = (++count).ToString();

        // Read input:
        Console.Write("Enter the number to search for: ");

        // Input a string:
        string myNumber = Console.ReadLine();

        // Search:
        for (int i = 0; i < x; i++)
        {
            for (int j = 0; j < y; j++)
            {
                if (array[i, j].Equals(myNumber))
                {
                    goto Found;
                }
            }
        }

        Console.WriteLine("The number {0} was not found.", myNumber);
        goto Finish;

    Found:
        Console.WriteLine("The number {0} is found.", myNumber);

    Finish:
        Console.WriteLine("End of search.");
    }
}
/*
Sample Input: 44

Sample Output
Enter the number to search for: 44
The number 44 is found.
End of search.
*/

Especificación del lenguaje C#

Para obtener más información, vea las secciones siguientes de Especificación del lenguaje C#.

  • 5.3.3.10 Instrucciones break, continue y goto

  • 8.9.3 La instrucción goto

Vea también

Conceptos

Guía de programación de C#

Referencia

Palabras clave de C#

The goto Statement

Instrucciones de salto (Referencia de C#)

Otros recursos

Referencia de C#