简体   繁体   English

如何退出 Java 中的 while 循环?

[英]How do I exit a while loop in Java?

What is the best way to exit/terminate a while loop in Java?在 Java 中退出/终止 while 循环的最佳方法是什么?

For example, my code is currently as follows:例如,我的代码目前如下:

while(true){
    if(obj == null){

        // I need to exit here

    }
}

Use break :使用break

while (true) {
    ....
    if (obj == null) {
        break;
    }
    ....
}

However, if your code looks exactly like you have specified you can use a normal while loop and change the condition to obj != null :但是,如果您的代码看起来与您指定的完全一样,您可以使用普通的while循环并将条件更改为obj != null

while (obj != null) {
    ....
}
while(obj != null){
  // statements.
}

break is what you're looking for: break是你要找的:

while (true) {
    if (obj == null) break;
}

alternatively, restructure your loop:或者,重组你的循环:

while (obj != null) {
    // do stuff
}

or:要么:

do {
    // do stuff
} while (obj != null);

You can do multiple condition logical tests within the while() check using the same rules as within any logical check.您可以使用与任何逻辑检查相同的规则在 while() 检查中执行多个条件逻辑测试。

while ( obj != null ) {  
    // do stuff  
}

works, as does工作,就像

while ( value > 5 && value < 10 ) {  
    // do stuff  
}  

are valid.是有效的。 The conditionals are checked on each iteration through the loop.在循环的每次迭代中检查条件。 As soon as one doesn't match, the while() loop is exited.一旦不匹配,就会退出 while() 循环。 You can also use break;你也可以使用 break;

while ( value > 5 ) {  
    if ( value > 10 ) { break; }  
    ...  
}  

Finding a while...do construct with while(true) in my code would make my eyes bleed.找个while...do在我的代码中用while(true)构造会让我的眼睛流血。 Use a standard while loop instead:改用标准的while循环:

while (obj != null){
    ...
}

And take a look at the link Yacoby provided in his answer , and this one too.并查看 Yacoby 在他的回答中提供的链接,还有这个链接。 Seriously.严重地。

The while and do-while Statements while 和 do-while 语句

You can use "break", already mentioned in the answers above.您可以使用上面的答案中已经提到的“break”。 If you need to return some values.如果你需要返回一些值。 You can use "return" like the code below:您可以像下面的代码一样使用“return”:

 while(true){
       if(some condition){
            do something;
            return;}
        else{
            do something;
            return;}
            }

in this case, this while is in under a method which is returning some kind of values.在这种情况下,这个 while 是在一个返回某种值的方法下。

Take a look at the Java™ Tutorials by Oracle.查看 Oracle 的Java™ 教程

But basically, as dacwe said , use break .但基本上,正如dacwe 所说,使用break

If you can it is often clearer to avoid using break and put the check as a condition of the while loop, or using something like a do while loop.如果可以,避免使用 break 并将检查作为 while 循环的条件,或使用 do while 循环之类的东西通常会更清楚。 This isn't always possible though.但这并不总是可能的。

if you write while(true) .如果你写while(true) its means that loop will not stop in any situation for stop this loop you have to use break statement between while block.这意味着循环在任何情况下都不会停止,要停止此循环,您必须在 while 块之间使用 break 语句。

package com.java.demo;

/**
 * @author Ankit Sood Apr 20, 2017
 */
public class Demo {

    /**
     * The main method.
     *
     * @param args
     *            the arguments
     */
    public static void main(String[] args) {
        /* Initialize while loop */
        while (true) {
            /*
            * You have to declare some condition to stop while loop 

            * In which situation or condition you want to terminate while loop.
            * conditions like: if(condition){break}, if(var==10){break} etc... 
            */

            /* break keyword is for stop while loop */

            break;
        }
    }
}

You can use "Break;" 您可以使用“中断”。 in your code. 在您的代码中。

while (true) {
if (obj == null) break;

} }

您可以使用“break”来中断循环,这将不允许循环处理更多条件

To exit a while loop, use Break;要退出 while 循环,请使用Break; This will not allow to loop to process any conditions that are placed inside, make sure to have this inside the loop, as you cannot place it outside the loop这将不允许循环处理放置在内部的任何条件,请确保将其置于循环内,因为您不能将其置于循环外

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM