<分区>
Possible Duplicate:
Why is this code giving an “Unreachable Statement” error?
这个问题看起来很简单,我在一本书上找到了这个问题。如果有人帮我弄清楚为什么我会出错。
do {
System.out.print("inside do");
} while (false);
while (false) { // error
System.out.print("inside while");
}
System.out.print("outside");
我想,据我所知,输出应该在inside dooutside。但是,它显示编译器错误:无法访问语句。然后,我试图弄清楚为什么它显示 Compilation error : Unreachable Statement* 。所以,我把上面的代码改成这样
boolean i = false;
do {
System.out.print("inside do");
} while (false);
while (i) { // ok
System.out.print("inside while");
}
System.out.print("outside");
现在,它显示了预期的输出,即 inside dooutside 。所以,我的问题是 - 在第一种情况和第二种情况下有什么区别? 另外,当我检查
if(false){
//something here
}
然后,上面的代码执行没有任何错误。