如何继续工作? [英] How does continue work?

查看:122
本文介绍了如何继续工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解继续是如何运作的。我理解了关键字的概念,但是当我运行不同的程序时,它的工作方式不同: - /
让我给你看几个例子:

I am trying to understand how "continue" works. I understood the concept of the keyword but when I run different programs, it works differently :-/ Let me show you few examples:

如果我运行这个程序:

int j = 0;
int i = 0;
LABEL1: for (; i < 3; i++) {
  if (true)
    continue;
}

i的值将是3.到目前为止一直很好。
让我们添加一个外循环:

The value of i is gonna be 3. so far so good. Let's add an outer loop :

int j = 0;
int i = 0;
LABEL2: for (; j < 3; j++) {
  LABEL1: for (; i < 3; i++) {
    if (true)
      continue LABEL2;
  }
}

i的值将是... 0 !
我不明白为什么如果继续使用带有转到外部循环的标签,我不会增加。有人能解释一下为什么吗?
你有一些棘手的东西吗?还是做了一段时间?

The value of i is gonna be ... 0 !! I don't understand why i is not incremented if continue is used with a label that goes to the outer loop. Can somebody explain it why ? Do you have some tricky things like that with break ? or with a do {} while ?

我非常感谢您提供的任何帮助。

I really appreciate any help you can provide.

推荐答案

语句结构的基本如下:

The basic for statement structure is as follows:


BasicForStatement:
for ( [ForInit] ; [Expression] ; [ForUpdate] ) Statement

现在,来自 JLS§14.14.1.3。突然完成 声明


如果由于 继续没有标签语句的执行突然完成,然后按顺序执行以下两个步骤:

If execution of the Statement completes abruptly because of a continue with no label, then the following two steps are performed in sequence:


  1. 首先,如果 ForUpdate 部分存在,则表达式被评估按顺序从左到右;它们的值(如果有的话)被丢弃。如果 ForUpdate 部分不存在,则不采取任何措施。

  1. First, if the ForUpdate part is present, the expressions are evaluated in sequence from left to right; their values, if any, are discarded. If the ForUpdate part is not present, no action is taken.

其次 for 执行迭代步骤。

如果执行 Statement 完成由于 继续突然因为标签 L ,所以有一个选择:

If execution of the Statement completes abruptly because of a continue with label L, then there is a choice:


  • 如果语句的标签为 L ,然后按顺序执行以下两个步骤:

  • If the for statement has label L, then the following two steps are performed in sequence:


  1. 首先,如果 ForUpdate 部分存在,表达式按从左到右的顺序进行评估;它们的值(如果有的话)被丢弃。如果 ForUpdate 不存在,则不采取任何措施。

  1. First, if the ForUpdate part is present, the expressions are evaluated in sequence from left to right; their values, if any, are discarded. If the ForUpdate is not present, no action is taken.

其次 执行迭代步骤。


  • 如果对于语句没有标签 L ,语句的突然完成,因为继续标签 L

  • If the for statement does not have label L, the for statement completes abruptly because of a continue with label L.

    (强调我的)

    ForUpdate ,在你的情况下,是 i ++ 。基于以上内容:

    ForUpdate, in your case, is i++. Based on what's above:


    • 您的第一个片段属于第一种情况,因此 i 递增。

    你的第二个片段属于第二种情况,所以 i 是没有增加,因为语句的突然完成。

    Your second snippet falls under the second case, so i is not incremented because the for statement completes abruptly.

    注意如果你的第二个片段中有继续LABEL1 ,那么 i 会像你的第一个片段一样增加(按照使用JLS)。

    Note that if you had continue LABEL1 in your second snippet, i would have been incremented as in your first snippet (in accordance with the JLS).

    作为未来的提示,对于语言规则/语义的权威性答案,你应该总是参考语言规范。对于Java,这是 JLS

    As a tip for the future, for definitive answers regarding language rules/semantics, you should always consult the language specification. For Java, that's the JLS.

    这篇关于如何继续工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆