do-while with 语句在最后

do-while with statement at the end

有人可以让我知道一些定义这种奇怪的同时执行行为的在线资源吗?

int i = 0;
do while ( ++i < 1 ) { //this compile (?!?)
      System.out.print("i is " + i);
}

do while ( ++i < 1 )  // this compile also
      System.out.print("i is " + i);
do while ( i > 1 ) {}

while ( i > 1 ) {} //this doesn't compile, the comp. wants the semicolon

抱歉,我在 do-while 语句中遗漏了什么?

Oracle 官方link 完全没有提到这一点: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

编辑: 抱歉,我将嵌套的 while 解释为另一条语句。

这是让我感到困惑的原始陈述:

int i = 1;
do while ( i < 1 )
System.out.print("i is " + i);
while ( i > 1 );

此编译...等同于:

do {
    while ( ++i < 1 )  // this compile also!
    System.out.print("i is " + i);
}
while ( i > 1 );

在oracle中link你贴的都解释了:

The Java programming language also provides a do-while statement, which can be expressed as follows:

do {
     statement(s)
} while (expression);

如果我在 IDE (Eclipse) 上复制您的代码,这是我的结果:

int i = 0;
do while ( ++i < 1 ) { //DO NOT COMPILE,Syntax error, insert "while ( Expression ) ;" to complete DoStatement
      System.out.print("i is " + i);
}

do while ( ++i < 1 )  // DO NOT COMPILE,Syntax error, insert "while ( Expression ) ;" to complete DoStatement
      System.out.print("i is " + i);

while ( i > 1 ) {} //COMPILE
do while ( i > 1 ) {} //DO NOT COMPILE,Syntax error, insert "while ( Expression ) ;" to complete DoStatement

我不确定你在问什么。

while(i>1){}//this doesn't compile.

语法正确。

另外,do-while 代码行的正确语句是

do {
     //code goes here
} while (i > 1);

我想我应该指出,不管怎样,do while 代码行都会执行括号内的代码,然后检查它是否需要重复。但是,while 语句将检查它是否 > 1,如果为真,它将 运行 代码。

希望对您有所帮助。

语法不正确时执行。 以更简单的方式,您应该这样想->做某事直到(条件) 所以这是语法

do{
//statement
}while(condition is true);