我什么时候应该使用“while 循环”?

发布于 2024-12-03 19:46:14 字数 324 浏览 0 评论 0原文

我刚刚偶然发现了这个问题 “while(true)”循环那么糟糕吗?
它们让我思考我通常会做什么。令我惊讶的是,我意识到我几乎从未在我的专业代码(或工作代码)中使用过 while 循环
前端框架例如面孔等不计入在内。
那么什么时候应该使用“while 循环”呢?和 您多久使用一次 while 循环?
这是一个真正的问题,请不要因为主观而关闭,我真的在寻找一个具体的例子。它不能用更好的替代方案代替。

I just stumbled upon this question Are "while(true)" loops so bad?

They made me think what do I normally do.And to my surprise I realised that I have almost never used a while loop in my professional code(or work code) .

Front end frameworks e.g faces etc do not count.

So When should I use a 'while loop'? and
How often do you use while loop?
It's is a real question please do not close as being subjective I really am after a concrete example.where it can not be replaced with a better alternate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

我要还你自由 2024-12-10 19:46:14

我可能会使用它的一个地方是您需要以不同于其他元素的方式对待序列的第一个元素。这使得 foreach 循环变得很尴尬,但 while 循环效果很好:

Iterator<String> iterator = foo.iterator();

// Handle the first item specially
if (!iterator.hasNext()) {
    throw new SomeException("foo shouldn't be empty");
}
SomeResult result = new SomeResult(iterator.next());

// Now deal with the rest
while (iterator.hasNext())
{
    String item = iterator.next();
    result.addItem(item);
}

此外,我还使用 while 循环作为少数几个在条件中包含赋值的地方之一:

String line;
while ((line = reader.readLine()) != null)
{
    // Handle the line
}

或使用 >输入流

int bytesRead;
while ((bytesRead = input.read(buffer)) != -1)
{
    // Handle the buffer
}

One place where I might use it is where you need to treat the first element of a sequence differently to the rest. That makes a foreach loop awkward, but a while loop works well:

Iterator<String> iterator = foo.iterator();

// Handle the first item specially
if (!iterator.hasNext()) {
    throw new SomeException("foo shouldn't be empty");
}
SomeResult result = new SomeResult(iterator.next());

// Now deal with the rest
while (iterator.hasNext())
{
    String item = iterator.next();
    result.addItem(item);
}

Also I use a while loop as one of the few places where I'll also include an assignment in a condition:

String line;
while ((line = reader.readLine()) != null)
{
    // Handle the line
}

or with an InputStream:

int bytesRead;
while ((bytesRead = input.read(buffer)) != -1)
{
    // Handle the buffer
}
遇见了你 2024-12-10 19:46:14
java.util.Scanner scanner = //...

while(scanner.hasNextLine()) {
  String line = scanner.nextLine();
  //..do sth with the line
}

事实上,每个 while 循环都可以用 for 替换。但是,例如在上面的代码中,它的可读性会较差 - 这就是要点:当它更适合问题的本质时,使用 while

java.util.Scanner scanner = //...

while(scanner.hasNextLine()) {
  String line = scanner.nextLine();
  //..do sth with the line
}

In fact every while loop can be replaced with for. But e.g. in the code above it would be less readable - and that's the point: use while when it fits better to the nature of the problem.

救星 2024-12-10 19:46:14

当某些条件成立时,您应该使用它来循环。

You should use it to loop while some condition holds true.

野却迷人 2024-12-10 19:46:14

简单永不停歇的后端逻辑:

while (true) {
  consumeMessage();
}

或者也

for (;;) {
  consumeMessage();
}

Simple never-stopping backend logic:

while (true) {
  consumeMessage();
}

Or also

for (;;) {
  consumeMessage();
}
葬花如无物 2024-12-10 19:46:14

当您不知道需要多少次迭代时,应该使用它。
你只知道当你的条件得到满足时你想做某事。它可以迭代 2、100、0... 次。

You should use it when you dont know how many iterations will be needed.
You only know that you want to do something while your condition is met. It could be itereated 2, 100, 0... times.

生生漫 2024-12-10 19:46:14

当然,您始终可以将 while 循环重写为 for 循环,但通常它更难看,这意味着 for (..;..;..) 的部分留空 - 主要是初始化。 Findbugs 在这种情况下也会给出警告:类似于“检测到简单的 for 循环,将其重写为 while 循环” 。

while 循环的主要应用是您不需要初始化,或者想要特殊对待第一次循环迭代(例如枚举的第一个元素),在这种情况下您也可以预先进行初始化。

Of course you can always rewrite a while loop into a for loop, but often it is uglier, meaning that parts of the for (..;..;..) are left blank - mainly the initialization. Findbugs also gives a warning in this case: similar to "simple for loop detected, rewrite it as a while loop".

The main application of the while loop is that you do not need an initialization, or want to treat the first loop iteration (e.g. first element of an enumeration) specially, in which case you do the initialization beforehand, too.

养猫人 2024-12-10 19:46:14

当您的代码中有一个主循环并且您想要运行它直到发生变化时,请使用它。

当您不需要计数器时,以及当您不需要迭代集合时(因为那时您需要计数器)。

使用 for(;whatever;) 是丑陋的代码,这就是你必须使用一段时间的地方。

另外,do ... while 的变体允许您至少执行某件事一次,然后可能执行多次。

Use it when you have a main loop in your code which you want to run until something changes.

When you dont need a counter, and when you dont need to iterate over a collection (because then you need a counter).

Using a for(;whatever;) is ugly code, thats where you have to use a while.

Also the variation, do ... while allows you to do something at least once and then possibly many times.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文