0

对于嵌套循环,当我continue与标签一起使用时,它会在编译期间给我一个错误,说the declared loop is not present.

特别是在这种情况下,显示的错误消息是:Second is not a loop label.

这是我为演示我的问题而编写的一段代码:

//using break as a form of GOTO 
class demo
{

    public static void main(String [] args)
    {   
        boolean b=false;
        First:{
            Second:{
                Third:{
                        System.out.println("Before BReak");
                        if(b)                               
                        continue Third;                 
                        else
                        break Second;
                    }
                    System.out.println("THis won't execute");
                }
                System.out.println("THis too won't Execute");
            }
    }   

}
4

3 回答 3

3

由于您不在某种循环中,因此您不能使用continue.

文档

continue 语句跳过 for、while 或 do-while 循环的当前迭代

于 2018-08-29T15:01:31.027 回答
0

这会导致 Java 中的编译错误,因为没有循环。continue 仅在存在循环时才有意义

于 2018-08-29T15:08:40.927 回答
0

你可以在没有循环的情况下使用 Label 语句,只有 break 而不是 continue

  • 使用 continue 你需要一个循环

  • 没有循环和中断语句的标签语句示例:

    总和块:{

         if (a < 0) {
                break sumBlock;
            }
    
            if (b < 0) {
                break sumBlock;
            }
            return a + b;
        }
    
于 2018-08-29T15:17:36.353 回答