How do I exit a while loop in Java?

后端 未结 10 2565
情话喂你
情话喂你 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 04:08

    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.

提交回复
热议问题