在 Java 中使用 break 退出循环是不好的做法吗? [英] Is it bad practice to use break to exit a loop in Java?

查看:30
本文介绍了在 Java 中使用 break 退出循环是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道使用 break 语句退出循环而不是满足循环条件是否是一种不好的做法"?

I was wondering if it is a "bad practice" to use a break statement to exit a loop instead of fulfilling the loop condition?

我对 Java 和 JVM 没有足够的了解,无法了解循环的处理方式,所以我想知道这样做是否忽略了一些关键的东西.

I do not have enough insight in Java and the JVM to know how a loop is handled, so I was wondering if I was overlooking something critical by doing so.

这个问题的重点:有没有具体的性能开销?

The focus of this question: is there a specific performance overhead?

推荐答案

老天爷没有.有时,循环中可能会出现满足总体要求的情况,但不满足逻辑循环条件.在这种情况下,使用 break 来阻止你毫无意义地循环.

Good lord no. Sometimes there is a possibility that something can occur in the loop that satisfies the overall requirement, without satisfying the logical loop condition. In that case, break is used, to stop you cycling around a loop pointlessly.

例子

String item;

for(int x = 0; x < 10; x++)
{
    // Linear search.
    if(array[x].equals("Item I am looking for"))
    {
       //you've found the item. Let's stop.
       item = array[x];
       break; 
    }
}

在这个例子中什么更有意义.每次都继续循环到 10,即使在您找到它之后,还是循环直到找到该项目并停止?或者把它变成现实世界的术语;当你找到你的钥匙时,你会继续寻找吗?

What makes more sense in this example. Continue looping to 10 every time, even after you've found it, or loop until you find the item and stop? Or to put it into real world terms; when you find your keys, do you keep looking?

根据评论进行编辑

为什么不将 x 设置为 11 以打破循环?没有用.我们有break!除非您的代码假设 x 稍后肯定大于 10(并且可能不应该),否则您只需使用 break.

Why not set x to 11 to break the loop? It's pointless. We've got break! Unless your code is making the assumption that x is definitely larger than 10 later on (and it probably shouldn't be) then you're fine just using break.

为完整起见进行编辑

肯定还有其他方法可以模拟break.例如,为循环中的终止条件添加额外的逻辑.说它要么毫无意义地循环要么使用 break 是不公平的.正如所指出的,while 循环通常可以实现类似的功能.例如,按照上面的例子..

There are definitely other ways to simulate break. For example, adding extra logic to your termination condition in your loop. Saying that it is either loop pointlessly or use break isn't fair. As pointed out, a while loop can often achieve similar functionality. For example, following the above example..

while(x < 10 && item == null)
{
    if(array[x].equals("Item I am looking for"))
    {
        item = array[x];
    }

    x++;
}

使用 break 只是意味着您可以使用 for 循环来实现此功能.这也意味着无论何时您希望循环的行为不同,您都不必在终止逻辑中不断添加条件.例如.

Using break simply means you can achieve this functionality with a for loop. It also means you don't have to keep adding in conditions into your termination logic, whenever you want the loop to behave differently. For example.

for(int x = 0; x < 10; x++)
{
   if(array[x].equals("Something that will make me want to cancel"))
   {
       break;
   }
   else if(array[x].equals("Something else that will make me want to cancel"))
   {
       break;
   }
   else if(array[x].equals("This is what I want"))
   {
       item = array[x];
   }
}

而不是具有如下终止条件的 while 循环:

Rather than a while loop with a termination condition that looks like this:

while(x < 10 && !array[x].equals("Something that will make me want to cancel") && 
                !array[x].equals("Something else that will make me want to cancel"))

这篇关于在 Java 中使用 break 退出循环是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆