Java 中的 While 循环怪异
我注意到java(因此可能是C)对此没有问题:
while(condition1) {
//do somethin'
} while(condition2);
这是否与:
while(condition1 && condition2) {
//do somethin'
}
I noticed that java (hence probably C) has no problem with this:
while(condition1) {
//do somethin'
} while(condition2);
Is this the same as:
while(condition1 && condition2) {
//do somethin'
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,你有两个循环。
第二个循环与编辑相同
:我的建议是定期使用 IDE 中的自动格式化程序。否则,您可以创建表明代码执行其不执行的操作的格式。
示例 1
在此示例中,前两个语句似乎是 if 条件的一部分,但只有第一个是。
示例 2
看起来不像合法的 Java,但它确实如此,原因并非格式所示;)
No, you have two loops.
The second loop is the same as
EDIT: My suggestion is to use the automatic formatter in your IDE regularly. Otherwise you can create formatting which suggests the code does things it doesn't.
example 1
In this example, it appears that the first two statements are part of the if condition, but only the first is.
example 2
Doesn't look like legal Java, but it is, not for the reasons the formatting suggests ;)
不,他们是不同的。
第一个
while(condition1)
将首先运行。然后是
while(condition2)
,除了一个;
之外什么都没有,这意味着它只是一些空语句。请记住,在
if
、for
、while
等控制块中,如果不使用{}
大括号,那么只有紧随其后的第一个语句才会被视为其中的一部分。示例:
编辑 Java 代码中提到了空 while 循环惯例
No, they are different.
The first
while(condition1)
will run first.Then comes
while(condition2)
, which has nothing after it except a single;
which means it's just some empty statement.Remember that in control blocks like
if
,for
,while
, if you don't use the{}
braces, then only the first immediate statement after it will be considered part of it.Example:
Edit The empty while loop is mentioned in the Java code conventions
java 没有构造,如第一种形式所示。您可能已经看到
编辑:您误读了第一种形式。
}
之后应该有一个换行符。这也让我很困惑。There is no construct is java as shown in the first form. You have probably seen
EDIT : You are misreading the first form. There should have been a line break after the
}
. This confused me as well.