Java 中的 While 循环怪异

发布于 2024-10-09 18:50:27 字数 219 浏览 0 评论 0原文

我注意到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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落花浅忆 2024-10-16 18:50:27

不,你有两个循环。

while(condition1) {
  // do something
}

while(condition2); // second loop which does nothing.

第二个循环与编辑相同

while(condition2) { }

:我的建议是定期使用 IDE 中的自动格式化程序。否则,您可以创建表明代码执行其不执行的操作的格式。

示例 1

if (condition)
    statement1;
    statement2;
statement3;

在此示例中,前两个语句似乎是 if 条件的一部分,但只有第一个是。

示例 2

http://www.google.com/
statement;

看起来不像合法的 Java,但它确实如此,原因并非格式所示;)

No, you have two loops.

while(condition1) {
  // do something
}

while(condition2); // second loop which does nothing.

The second loop is the same as

while(condition2) { }

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

if (condition)
    statement1;
    statement2;
statement3;

In this example, it appears that the first two statements are part of the if condition, but only the first is.

example 2

http://www.google.com/
statement;

Doesn't look like legal Java, but it is, not for the reasons the formatting suggests ;)

執念 2024-10-16 18:50:27

不,他们是不同的。

第一个 while(condition1) 将首先运行。

然后是 while(condition2),除了一个 ; 之外什么都没有,这意味着它只是一些空语句。

请记住,在 ifforwhile 等控制块中,如果不使用 {} 大括号,那么只有紧随其后的第一个语句才会被视为其中的一部分。

示例:

if (condition)
    System.out.println("hello"); // prints only if condition is true.
    System.out.println("no"); // not bound to the 'if'. Prints regardless.

while (condition)
    ; // do nothing!
    System.out.println("something"); // not bound to the while

编辑 Java 代码中提到了空 while 循环惯例

7.6 while 语句

while 语句应采用以下形式:

while (condition) {
    statements;
}

空的 while 语句应具有以下形式:

while (condition);

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:

if (condition)
    System.out.println("hello"); // prints only if condition is true.
    System.out.println("no"); // not bound to the 'if'. Prints regardless.

while (condition)
    ; // do nothing!
    System.out.println("something"); // not bound to the while

Edit The empty while loop is mentioned in the Java code conventions

7.6 while Statements

A while statement should have the following form:

while (condition) {
    statements;
}

An empty while statement should have the following form:

while (condition);
放肆 2024-10-16 18:50:27

java 没有构造,如第一种形式所示。您可能已经看到

do {

} while (cond)

编辑:您误读了第一种形式。 } 之后应该有一个换行符。这也让我很困惑。

There is no construct is java as shown in the first form. You have probably seen

do {

} while (cond)

EDIT : You are misreading the first form. There should have been a line break after the }. This confused me as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文