How do I exit a while loop in Java?

后端 未结 10 2490
情话喂你
情话喂你 2020-11-27 03:50

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

                


        
10条回答
  •  情深已故
    2020-11-27 04:12

    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.

提交回复
热议问题