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

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

    }
}

This question is related to java while-loop exit break

The answer is


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


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.


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

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.


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


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

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


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

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

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to while-loop

While, Do While, For loops in Assembly Language (emu8086) MySQL Insert with While Loop Python loop to run for certain amount of seconds How to break a while loop from an if condition inside the while loop? How to find sum of several integers input by user using do/while, While statement or For statement Python: How to keep repeating a program until a specific input is obtained? Creating multiple objects with different names in a loop to store in an array list ORA-06502: PL/SQL: numeric or value error: character string buffer too small How to break out of a loop in Bash? for or while loop to do something n times

Examples related to exit

Ruby - ignore "exit" in code Difference between return 1, return 0, return -1 and exit? Android: Quit application when press back button How to exit a function in bash break/exit script How to use sys.exit() in Python exit application when click button - iOS How to properly exit a C# application? What is the command to exit a Console application in C#? Get exit code for command in bash/ksh

Examples related to break

How to break a while loop from an if condition inside the while loop? illegal use of break statement; javascript How can I use break or continue within for loop in Twig template? break statement in "if else" - java Regarding Java switch statements - using return and omitting breaks in each case Is it bad practice to use break to exit a loop in Java? break/exit script Breaking out of a for loop in Java How to break out of while loop in Python? How to kill a while loop with a keystroke?