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

查看:169
本文介绍了在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 来打破循环?这是毫无意义。我们有休息!除非你的代码假设 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天全站免登陆