java Java中没有主体的while循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25772774/
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
While loop without a body in Java
提问by Shirgill Farhan
The body of the while(or any other of Java's loops) can be empty. This is because a null statement (one that consists only of a semicolon) is syntactically valid in Java. Now consider this :
while(或任何其他 Java 循环)的主体可以为空。这是因为 null 语句(仅由分号组成的语句)在 Java 中在语法上是有效的。现在考虑一下:
i = 100
j = 200
while(++i < --j); // no body in this loop
system.out.println(i);
system.out.println(j);
I noticed that if the value of i is less than j, then the loop repeats itself and when the condition fails to fulfill, then only the 2 below statements are exceuted. Am i correct or is there any other logic behind this?
我注意到,如果 i 的值小于 j,则循环会自我重复,当条件不满足时,则仅执行以下 2 个语句。我是正确的还是这背后有任何其他逻辑?
回答by rgettman
Yes, it continues to repeat itself without executing any other statements.
是的,它继续重复自己而不执行任何其他语句。
It will test the conditions 101 < 199
, 102 < 198
, and so on, until it tests 150 < 150
. Then it breaks out of the loop and executes the println
statements.
它将测试条件101 < 199
、102 < 198
等等,直到它测试150 < 150
。然后它跳出循环并执行println
语句。
Output:
输出:
150
150
Note that the only reason it's not an infinite loop is that the condition is itself changing the values with the ++
and --
operators.
请注意,它不是无限循环的唯一原因是条件本身使用++
和--
运算符更改了值。
回答by Simone Gianni
Answers already given are correct.
已经给出的答案是正确的。
However i want to point out that it's not true that the loop has no body. You have to consider it once compiled. It becomes :
但是我想指出循环没有主体是不正确的。一旦编译,你必须考虑它。它成为了 :
- Initialize i at 100
- Initialize j at 200
- Increment i
- Decrement j
- IF NOT(I < J) jump to 7
- Jump to 3
- Print and stuff
- 将 i 初始化为 100
- 将 j 初始化为 200
- 增量 i
- 递减 j
- IF NOT(I < J) 跳到 7
- 跳到 3
- 打印和东西
The loop in this case is from 3 to 6, and has a body (increment i decrement j) and a condition check at point 5.
这种情况下的循环是从 3 到 6,并且有一个主体(增量 i 减 j)和点 5 处的条件检查。
To make it clearer, I will write another loop, functionally identical, but with a body :
为了更清楚,我将编写另一个循环,功能相同,但有一个主体:
while (true) {
i++;
j--;
if (!(i<j)) break;
}
Now this loop has a body, but probably the compiled version of this loop and the previous one are identical.
现在这个循环有一个主体,但这个循环的编译版本可能与前一个循环相同。
However, we could mean by "Empty body loop" a loop that has no side effects. For example, this loop :
然而,我们可以说“空体循环”是一个没有副作用的循环。例如,这个循环:
for (int i = 0; i < 10000; i++);
This loop is "really" empty : no matter if you execute it or not, it doesn't give or take anything from the rest of the program (if not a delay).
这个循环“真的”是空的:无论你是否执行它,它都不会从程序的其余部分提供或获取任何东西(如果不是延迟的话)。
So, we could define it "empty of any meaning" more than "empty of any body".
因此,我们可以将其定义为“没有任何意义”,而不是“没有任何身体”。
In this case, the JIT will notice it, and wipe it out.
在这种情况下,JIT 会注意到它,并将其清除。
In this sense, your loop could be :
从这个意义上说,您的循环可能是:
for (int i = 100, j = 200; i < j; i++, j--);
Here, since "i" and "j" are declared "inside" the loop (doesn't exist outside it's scope), then this loop is meaningless, and will be wiped out.
在这里,由于“i”和“j”被声明在循环的“内部”(不存在于它的范围之外),那么这个循环是没有意义的,将被清除。
Maybe this is what Herbert Shildt (that I admit I don't know) means?
也许这就是 Herbert Shildt(我承认我不知道)的意思?
Also see here Java: how much time does an empty loop use?about empty loops and JIT.
另请参见此处Java:空循环使用了多少时间?关于空循环和 JIT。
回答by Elliott Frisch
Yes. The boolean expression will evaluate at each iteration, and when it results in false the loop will terminate. Also, it's System.out.println
not system.out.println
.
是的。布尔表达式将在每次迭代时求值,当结果为 false 时,循环将终止。此外,它System.out.println
不是system.out.println
。
回答by brso05
You are correct. The loop will execute the loop body (there is no loop body) until the while condition is met then it will break out of the loop and execute the next 2 statements in sequence.
你是对的。循环将执行循环体(没有循环体),直到满足 while 条件,然后它会跳出循环并依次执行接下来的 2 条语句。
回答by Simon Baars
I would like to add that you should omit the indentation for the two statements (println statements) below the while loop. You should not see this as a conditional, like an if-statement.
我想补充一点,您应该省略 while 循环下方的两个语句(println 语句)的缩进。您不应将此视为条件,例如 if 语句。