Java 继续工作吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2016513/
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
does continue work in a do while?
提问by mrblah
I have a do while that looks like:
我有一段时间看起来像:
User user = userDao.Get(1);
do
{
// processing
// get the next user
//
user = UserDao.GetNext(user.Id);
if(user == null)
continue; // will this work?????????????
}
while ( user != null)
If it does work, its going to go to the top of the do statement, and user is null so things are going to break?
如果它确实有效,它会转到 do 语句的顶部,并且 user 为空,所以事情会崩溃吗?
Maybe I should rework the loop to a while statement?
也许我应该将循环改写为 while 语句?
采纳答案by helios
The continue makes it jump to the evaluation at the botton so the program can evaluate if it has to continue with another iteration or exit. In this case it will exit.
continue 使它跳转到底部的评估,因此程序可以评估它是否必须继续另一个迭代或退出。在这种情况下,它将退出。
This is the specification: http://java.sun.com/docs/books/jls/third_edition/html/statements.html#6045
这是规范:http: //java.sun.com/docs/books/jls/third_edition/html/statements.html#6045
Such language questions you can search it in the Java Language Specification: http://java.sun.com/docs/books/jls/
此类语言问题您可以在 Java 语言规范中搜索:http: //java.sun.com/docs/books/jls/
回答by Andres
The continue will jump to the loop evaluation statement inside the while which looks redundant as your if and while are evaluating the same thing . I think it would be better to use a break or simply let the while condition do the work (your while condition is already checking it for you)
continue 将跳转到 while 内的循环评估语句,这看起来是多余的,因为 if 和 while 正在评估相同的东西。我认为最好使用休息或简单地让 while 条件完成工作(您的 while 条件已经在为您检查)
回答by Rich Adams
Yes, continue
will work in a do..while loop.
是的,continue
将在 do..while 循环中工作。
You probably want to use break
instead of continue
to stop processing the users, or just remove the if null continue
bit completely since the while loop will break out as soon as user is null anyway.
您可能想要使用break
而不是continue
停止处理用户,或者只是if null continue
完全删除该位,因为一旦 user 为空,while 循环就会中断。
回答by patros
Short answer yes, continue (and break) work properly in do while loops.
简短的回答是,继续(和中断)在 do while 循环中正常工作。
As others have pointed out though, from the example it looks like you may be expecting the wrong behavior from continue.
正如其他人指出的那样,从示例中看起来您可能期望继续执行错误的行为。
回答by alphazero
Why are you testing user in two places? The while condition will terminate the loop when user is null, which is what you want.
为什么要在两个地方测试用户?当用户为空时,while 条件将终止循环,这正是您想要的。
回答by Jamie McCrindle
This really wouldn't be the best way to write this code. If user is null, you'll get a NullPointerException when you try and get user.id the next time around. A better way to do this would be:
这真的不是编写此代码的最佳方式。如果 user 为 null,则在下次尝试获取 user.id 时会得到 NullPointerException。一个更好的方法是:
User user = UserDao.Get(1);
while(user != null) {
// do something with the user
user = UserDao.GetNext(user.id);
}
回答by OscarRyz
Let's see:
让我们来看看:
$cat DoWhileContinue.java
class DoWhileContinue {
public static void main( String [] args ) {
int i = 0;
int y = 0;
do {
i++;
if( i > 100 ) {
continue;
}
y++;
} while( i < 500 );
System.out.printf("i=%d, y=%d %n", i, y );
}
}
$javac DoWhileContinue.java
$java DoWhileContinue
i=500, y=100
Yes it does. In this sample you can see y
value is 100
because the continue
statement was executed and prevented the variable from being increased afterward.
是的,它确实。在此示例中,您可以看到y
value 是100
因为该continue
语句被执行并阻止了之后的变量增加。