I don't understand the output of the following Java snippet with break + labels (like goto in other languages)

huangapple go评论101阅读模式
英文:

I don't understand the output of the following Java snippet with break + labels (like goto in other languages)

问题

我不理解以下代码的输出:

public class testBreak
{
    public static void runTest()
    {
        high_level:
        {
            System.out.println("Enter in High Level");
        middle_level: 
            for(int i = 0; i < 4; i++)
            {
                System.out.println("Enter in Middle level cicle");
                int j = 0;
            low_level: 	
                while(j++ < 2)
                {
                    System.out.println("Enter in Low level cicle");
                    System.out.println("i = " + i + "; j = " + j);
                    switch(i)
                    {
                        case 0 : break;
                        case 1 : break low_level;
                        case 2 : break middle_level;
                        case 3 : break high_level;
                    }
                    System.out.println("Exit from Low level cicle");
                }
                System.out.println("Exit from Middle level cicle");
            }
            System.out.println("Exit from High level cicle");
        }
    }

    public static void main(String args[])
    {
        runTest();
    }
}

以下是输出结果:

Enter in High Level
Enter in Middle level cicle
Enter in Low level cicle
i = 0; j = 1
Exit from Low level cicle
Enter in Low level cicle
i = 0; j = 2
Exit from Low level cicle
Exit from Middle level cicle
Enter in Middle level cicle
Enter in Low level cicle
i = 1; j = 1
Exit from Middle level cicle
Enter in Middle level cicle
Enter in Low level cicle
i = 2; j = 1
Exit from High level cicle

我理解到 i = 1; j = 1,但为什么它会跳到 Exit from Middle level cicle 而不是 Enter in low level cicle 呢?因为我们有 j = 1 并且 while(j++ < 2) 应该成立(1 < 2),然后增加 j 到 2 并进入低级循环,就像在第一次运行时一样?

编辑:看起来使用标签的 break 不等同于 goto,它会跳出带有标签的循环?并且该循环不像在C/C++中一样自由地存在于代码中,而是应用于特定的循环?

英文:

I don't understand the output of the following code:

public class testBreak
{

	public static void runTest()
	{
	high_level:
		{
			System.out.println(&quot;\nEnter in High Level&quot;);
		middle_level: 
			for(int i = 0; i &lt; 4; i++)
			{
				System.out.println(&quot;Enter in Middle level cicle&quot;);
				int j = 0;
			low_level: 	
				while(j++ &lt; 2)
				{
					System.out.println(&quot;Enter in Low level cicle&quot;);
					System.out.println(&quot;i = &quot; + i + &quot;;j = &quot; + j);
					switch(i)
					{
						case 0 : break;
						case 1 : break low_level;
						case 2 : break middle_level;
						case 3 : break high_level;
					}
					System.out.println(&quot;Exit from Low level cicle&quot;);
				}
				System.out.println(&quot;Exit from Middle level cicle&quot;);
			}
			System.out.println(&quot;Exit from High level cicle&quot;);
		}
	}
	
	public static void main(String args[])
	{
		runTest();
	}

}

Here is the output:

Enter in High Level
Enter in Middle level cicle
Enter in Low level cicle
i = 0;j = 1
Exit from Low level cicle
Enter in Low level cicle
i = 0;j = 2
Exit from Low level cicle
Exit from Middle level cicle
Enter in Middle level cicle
Enter in Low level cicle
i = 1;j = 1
Exit from Middle level cicle
Enter in Middle level cicle
Enter in Low level cicle
i = 2;j = 1
Exit from High level cicle

I understand up to the line i = 1;j = 1
but why does it go to Exit from Middle level cicle and not to Enter in low level cicle because we have j = 1 and while(j++ < 2) should take 1 < 2, then increase j to 2 and enter the low level cicle as it did in the first run??

editet: It seems break with label does not equal goto, it breaks out of the labeled loop? and the loop is not free in the code like in C/C++, but it is applied to that particular loop?

答案1

得分: 0

i = 1时,你告诉它跳出low_level标签。在low_level下方的循环停止循环,然后打印Exit from Middle level cicle。通过这样做,你会再次在middle_level循环下方进行循环,这就是为什么它然后打印Enter in Middle level cicle

英文:

When i = 1, you are telling it to break out of the low_level label. The loop below low_level stops looping, and then prints Exit from Middle level cicle. By doing this, you then loop again below the middle_level loop, which is why it then prints Enter in Middle level cicle.

答案2

得分: 0

来自Oracle的Java教程:

break语句终止了带有标签的语句;它不会将控制流转移到标签处。控制流会转移到终止的带有标签的语句后面的语句。

这个回答符合您的问题吗?

英文:

Taken from the Java tutorial by Oracle:

> The break statement terminates the labeled statement; it does not
> transfer the flow of control to the label. Control flow is transferred
> to the statement immediately following the labeled (terminated)
> statement.

Does this answer your question?

huangapple
  • 本文由 发表于 2020年8月1日 21:46:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63205896.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定