简体   繁体   English

是否在for循环中使用break?

[英]To use break in a for loop or not?

Just one quick question, say, Car class extends HashMap(String, String). 只有一个简单的问题,比如,Car类扩展了HashMap(String,String)。

1. 1。

for (Car car : carList) {
    if (car.isEmpty) {
        break;
    }
    doSomething();
}

2. 2。

for (Car car : carList) {
    if (!car.isEmpty) {
        doSomethingElse();
    }
}

Which of the above two is better? 以上两种哪种更好? Thanks. 谢谢。

---- edited ---- Sorry I did not make my point clear. ----编辑----对不起,我没有说清楚。

The doSomething() method is actually doing different things. doSomething()方法实际上做了不同的事情。 I've changed them to doSomething() and doSometingElse(). 我把它们改成了doSomething()和doSometingElse()。

My question is, will you put all the process in one if()? 我的问题是,你会把所有的过程放在一个if()吗? or first break the loop if the if() condition does not satisfy. 或者,如果if()条件不满足,则首先打破循环。

Thanks. 谢谢。

They do completely different things. 他们完全不同的事情。 The former will stop iterating as soon as the condition is true, whereas the latter will merely skip the processing during the iterations where the condition is false. 前者将在条件为真时立即停止迭代,而后者仅在条件为假的迭代期间跳过处理。

Changing the break to continue in the first will make them work the same way. 更改breakcontinue在第一个中将使它们以相同的方式工作。

They do completely different things. 他们完全不同的事情。 First one will stope after seeing first empty car. 第一个人在看到第一辆空车后会停下来。 Second version will 'do something' for each non-empty car. 第二个版本将为每辆非空车“做点什么”。 I think, you might want to use continue operator instead of break . 我想,您可能希望使用continue运算符而不是break

These 2 loops don't do the same thing 这两个循环不会做同样的事情

  1. will terminate the loop the first time it finds an empty car with isEmpty skipping any cars after the first empty one. 将在第一次找到空车时终止循环,isEmpty在第一个空车后跳过任何车辆。 You can make this work like 2. if you change ' break ' to ' continue ' 如果你将' break '更改为' continue ',你可以使它像2.一样工作

  2. will doSomething for all not-empty cars. 将为所有非空车做点什么。

取决于你想要做什么,第一个循环将在条件满足时结束,而第二个循环将遍历所有散列映射。

If you actually meant "continue" and not "break", then I'd say not using continue or break is better. 如果你实际上意味着“继续”而不是“休息”,那么我会说不使用继续或休息更好。

It's not a huge deal, but statements like continue, break and return are all essentially goto statements that have been dressed up a little. 这不是一个大问题,但像继续,休息和返回这样的陈述都是基本上已经打扮得很少的goto语句。 Instead of goto a line or goto a label they goto the top or bottom of a control structure--it makes the programmer have to think a little bit when they track it which means wasted time and additional chances for errors. 它们不是转到一行或转到标签,而是转到控制结构的顶部或底部 - 这使得程序员在跟踪它时必须考虑一点,这意味着浪费时间和错误的额外机会。

It's not a terribly big deal, and many times these structures will actually clarify your code, but if you have a choice and they seem to do essentially the same thing, go the way that doesn't involve sending control elsewhere. 这不是一个非常大的交易,很多时候这些结构实际上会澄清你的代码,但是如果你有一个选择并且它们似乎基本上做同样的事情,那么就不要在其他地方发送控制权。

If you want different behavior based on if the car is empty or not, you can use an if else statement. 如果您想根据汽车是否为空而需要不同的行为,则可以使用if else语句。 Use break if you want to stop iterating carList. 如果要停止迭代carList,请使用break。

for (Car car : carList) {
   if (car.isEmpty) {
      doSomething();
   }
   else {
      doSomethingElse();
   }
}

As everyone has pointed out, the break version will exit early. 正如大家所指出的那样,破解版将提前退出。 The answer then depends on what you want it to do. 答案取决于你想要它做什么。

If you've finished doing what you need to do then break out as soon as you're done. 如果你已经完成了你需要做的事情,那么一旦你完成就会爆发。 Why waste CPU cycles? 为什么浪费CPU周期? on the other hand if you must go through the whole list to dosomethingelse() then don't break out. 另一方面,如果你必须通过整个列表dosomethingelse()然后不要爆发。

Were you meant to have two different named functions or should they both be called dosomething() ? 您是否打算使用两个不同的命名函数,还是应该将它们都称为dosomething()?

Is this what you're looking for? 这是你在找什么?

for (Car car : carList) {
    if (car.isEmpty) {
         doSomething();
    }
    else {
         doSomethingElse();
    }       
}

Or are you looking for 或者你在寻找

for (Car car : carList) {
    if (car.isEmpty) {
       break; 
       //STOP ITERATING THROUGH THE REST OF THE LIST 
       //(doSomething & soSomethingElse may have been called a few times already)
    }
    else {
        doSomething();
    }
    doSomethingElse();
}

There are other options as well, your clarification still isn't clear... to me anyway... 还有其他选择,你的澄清仍然不清楚......无论如何......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM