How do I exit a while loop in Java?

JavaWhile LoopExitBreak

Java Problem Overview


What is the best way to exit/terminate a while loop in Java?

For example, my code is currently as follows:

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

        // I need to exit here

    }
}

Java Solutions


Solution 1 - Java

Use 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) {
    ....
}

Solution 2 - Java

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

Solution 3 - Java

break is what you're looking for:

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

alternatively, restructure your loop:

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

or:

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

Solution 4 - Java

Finding a while...do construct with while(true) in my code would make my eyes bleed. Use a standard while loop instead:

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

And take a look at the link Yacoby provided in his answer, and this one too. Seriously.

The while and do-while Statements

Solution 5 - Java

You can do multiple condition logical tests within the while() check using the same rules as within any logical check.

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. You can also use break;

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

Solution 6 - Java

Take a look at the Java™ Tutorials by Oracle.

But basically, as dacwe said, use 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. This isn't always possible though.

Solution 7 - Java

You can use "break", already mentioned in the answers above. If you need to return some values. You can use "return" like the code below:

 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.

Solution 8 - Java

You can use "break" to break the loop, which will not allow the loop to process more conditions

Solution 9 - Java

To exit a while loop, use 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

Solution 10 - Java

if you write 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.

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;
		}
	}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBalaBView Question on Stackoverflow
Solution 1 - JavadacweView Answer on Stackoverflow
Solution 2 - JavaRizView Answer on Stackoverflow
Solution 3 - JavaChris JView Answer on Stackoverflow
Solution 4 - JavaXavi LópezView Answer on Stackoverflow
Solution 5 - JavaVikingGlenView Answer on Stackoverflow
Solution 6 - JavaYacobyView Answer on Stackoverflow
Solution 7 - JavaOmar Faroque AnikView Answer on Stackoverflow
Solution 8 - Javauser13366406View Answer on Stackoverflow
Solution 9 - JavaHussainOmerView Answer on Stackoverflow
Solution 10 - JavaAnkit SoodView Answer on Stackoverflow