📜  Java支持 goto 吗?

📅  最后修改于: 2022-05-13 01:55:40.440000             🧑  作者: Mango

Java支持 goto 吗?

Java不支持 goto,它被保留为关键字,以防万一他们想将其添加到更高版本中。

  • 与 C/C++ 不同, Java没有 goto 语句,但Java支持label
  • 标签在Java中唯一有用的地方就是嵌套循环语句之前。
  • 我们可以用 break 指定标签名称来打破特定的外循环。
  • 同样,可以使用 continue 指定标签名称。

在Java中使用带标签的break

Java
// Java code to illustrate
// using label  and break
// instead of goto
 
// file name: Main.java
public class Main {
    public static void main(String[] args)
    {
 
    // label for outer loop
    outer:
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (j == 1)
                    break outer;
                System.out.println(" value of j = " + j);
            }
        } // end of outer loop
    } // end of main()
} // end of class Main


Java
// Java code to illustrate
// using label  and continue
// instead of goto
 
// file name: Main.java
public class Main {
    public static void main(String[] args)
    {
 
    // label for outer loop
    outer:
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (j == 1)
                    continue outer;
                System.out.println(" value of j = " + j);
            }
        } // end of outer loop
    } // end of main()
} // end of class Main


Java
// Java code
public class Label_Break1 {
 
    public static void main(String[] args)
    {
 
        boolean t = true;
    first : {
    second : {
    third : {
        System.out.println("Before the break");
        if (t) // break out of second block
            break second;
    }
        System.out.println("This won't execute");
    }
        System.out.println("This is after the second block");
    }
    }
}
// This code is contributed by Sagar Gupta


Java
// Java code
public class Label_Break2 {
 
    public static void main(String[] args)
    {
 
    outer:
        for (int i = 0; i < 3; i++) // label
        {
            System.out.print("Pass " + i + ": ");
            for (int j = 0; j < 100; j++) {
                if (j == 10) {
                    break outer; // Exit both loops
                }
                System.out.print(j + " ");
            }
            System.out.println("This will not be printed");
        }
        System.out.println("Loops Complete.");
    }
}
// This code is contributed by Sagar Gupta


输出:
value of j = 0

在Java中使用 continue with label

我们也可以使用 continue 代替 break。例如,请参见以下程序。

Java

// Java code to illustrate
// using label  and continue
// instead of goto
 
// file name: Main.java
public class Main {
    public static void main(String[] args)
    {
 
    // label for outer loop
    outer:
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (j == 1)
                    continue outer;
                System.out.println(" value of j = " + j);
            }
        } // end of outer loop
    } // end of main()
} // end of class Main
输出:
value of j = 0
 value of j = 0
 value of j = 0
 value of j = 0
 value of j = 0
 value of j = 0
 value of j = 0
 value of j = 0
 value of j = 0
 value of j = 0

解释:由于 continue 语句跳到循环中的下一次迭代,它在 i 从 0 到 9 的迭代中迭代了 10 次。因此,外循环执行 10 次,而内 for 循环在每个外循环中执行 1 次。
Java没有 goto 语句,因为它提供了一种以任意和非结构化方式进行分支的方法。这通常会使 goto-ridden 的代码难以理解和维护。它还禁止某些编译器优化。然而,在某些地方,goto 是一种有价值且合法的流控制结构。例如,当您从一组深度嵌套的循环中退出时,goto 会很有用。为了处理这种情况, Java定义了 break 语句的扩展形式。
带标签的 break 语句的一般形式是:

break label;

示例 1:

Java

// Java code
public class Label_Break1 {
 
    public static void main(String[] args)
    {
 
        boolean t = true;
    first : {
    second : {
    third : {
        System.out.println("Before the break");
        if (t) // break out of second block
            break second;
    }
        System.out.println("This won't execute");
    }
        System.out.println("This is after the second block");
    }
    }
}
// This code is contributed by Sagar Gupta
输出:
Before the break
This is after the second block

示例 2:

Java

// Java code
public class Label_Break2 {
 
    public static void main(String[] args)
    {
 
    outer:
        for (int i = 0; i < 3; i++) // label
        {
            System.out.print("Pass " + i + ": ");
            for (int j = 0; j < 100; j++) {
                if (j == 10) {
                    break outer; // Exit both loops
                }
                System.out.print(j + " ");
            }
            System.out.println("This will not be printed");
        }
        System.out.println("Loops Complete.");
    }
}
// This code is contributed by Sagar Gupta
输出:
Pass 0: 0 1 2 3 4 5 6 7 8 9 Loops Complete.