在 Java 中 for 循环如何检查其条件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20450056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:01:44  来源:igfitidea点击:

How does a for loop check its conditions in Java?

javafor-loop

提问by itscharlieb

My question has to do with the order in which java checks the conditions of a for loop when there is a print statement inthe "conditions" of the loop. It seems like an unpractical thing to do (I haven't ever seen it used in any practical way), though my lack of understanding of what is printed has me thinking that I may not fully understand how a for loop functions. The following question showed up on a recent exam:

我的问题是关于在哪些Java检查的条件for循环时,有一个print语句的顺序做该循环的“条件”。这似乎是一件不切实际的事情(我从未见过以任何实际方式使用它),尽管我对打印的内容缺乏了解让我认为我可能没有完全理解 for 循环的功能。最近的一次考试中出现了以下问题:

What will the following method print with an input of n = 5?

以下方法将在输入 n = 5 时打印什么?

public static void mystery(int n) {
    for (int i = -1; i < n; System.out.print(i + " ")) {
         i++;
    }
}

The correct answer is: 0 1 2 3 4 5

正确答案是:0 1 2 3 4 5

To me, it seems that the loop ought to print -1, then increment i by 1, print 0 ..... until i = 4. Then it would print 4, increment i by 1, and break out of the loop at the loop's condition i < n. Why is the correct answer what it is and why is my logic flawed?

对我来说,循环似乎应该打印 -1,然后将 i 递增 1,打印 0 ..... 直到 i = 4。然后它会打印 4,将 i 递增 1,并在循环条件 i < n。为什么正确答案是什么,为什么我的逻辑有缺陷?

采纳答案by Sotirios Delimanolis

The initializer expression happens once. Then the condition is checked, then the body of the loop happens, then the increment expression happens (your printstatement), and then we start over.

初始化表达式发生一次。然后检查条件,然后循环体发生,然后增量表达式发生(你的print语句),然后我们重新开始。

The official tutorialis pretty clear if you read through it.

如果你通读它,官方教程很清楚。

The Java Language Specification entry for the forstatementmight also be interesting if you want complete details.

如果您需要完整的详细信息,该for语句Java 语言规范条目也可能很有趣。

回答by Steve P.

for (int i = -1; i < n; System.out.print(i + " ")) 
{
    i++;
}

First, iis initialized to-1(only happens once), and i < n, so we go into the loop. iis incremented inside of the for loop, so now i == 0. We then go to the print statement. Repeat.

首先,i被初始化为-1(只发生一次),和i < n,所以我们进入循环。 i在 for 循环内递增,所以现在i == 0. 然后我们转到打印语句。重复。

The expected behavior that you want would require something like:

您想要的预期行为需要类似于:

for (int i = -1; i < n; i++) 
{
    System.out.print(i + " ")
}

Here's a reference for the for statement.

这是for 语句的参考。

回答by Tim Destan

A for loop executes the increment step (the third part inside the parentheses) after each execution of the loop. If you switched your print statement and your i++, you would get the result you expect.

for 循环在每次执行循环后执行增量步骤(括号内的第三部分)。如果你切换了你的打印语句和你的 i++,你会得到你期望的结果。

回答by maybeWeCouldStealAVan

for ( <initialization> ; <test> ; <increment> ) {
     <body>
}

is equivalent to something like:

相当于:

<initialization>
while ( <test> ) {
    <body>
    <increment>
}

So you have:

所以你有了:

i = -1;
while ( i < n ) {
    i++;
    System.out.print(i + " ");
}

The "problem" in your test question is that the types of statements usually put in the <increment>and <body>portions are switched.

您的测试问题中的“问题”是通常放在<increment><body>部分中的语句类型被切换。

回答by Vipin

for(initialization; Boolean_expression; update)
{
   //Body
}

Here is the flow of control in a for loop:

这是 for 循环中的控制流程:

  1. The initialization step is executed first, and only once.

  2. Next, the Boolean expression is evaluated.

  3. If Boolean expression is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement past the for loop means loop is over.

  4. After the body of the for loop executes, the flow of control jumps back up to the update statement. Here your print statement is getting executed.

  5. The Boolean expression is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then update step, then Boolean expression). After the Boolean expression is false, the for loop terminates.

  1. 初始化步骤首先执行,并且只执行一次。

  2. 接下来,计算布尔表达式。

  3. 如果布尔表达式为真,则执行循环体。如果为 false,则循环体不执行,控制流跳转到 for 循环后的下一条语句,这意味着循环结束。

  4. 在 for 循环体执行后,控制流跳回到更新语句。在这里,您的打印语句正在执行。

  5. 现在再次计算布尔表达式。如果为真,则循环执行并重复该过程(循环体,然后是更新步骤,然后是布尔表达式)。在布尔表达式为假之后,for 循环终止。

For you below are the steps

以下是为您准备的步骤

in 1st step value of i is -1 at start.
in 2nd step i=-1 is less then n=5 so body will be executed.
in 3rd step i will be incremented to i=0
in 4th step value of i ( which is 0 gets printed)
in 5th step Boolean expression is evaluated again and it returns true as i=0 is less then n=5. so again step 3 ( Body of loop) is executed.