How do I exit a while loop in Java?

后端 未结 10 2517
情话喂你
情话喂你 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:06

    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

提交回复
热议问题