是“while(true)”循环这么糟糕? [英] Are "while(true)" loops so bad?

查看:140
本文介绍了是“while(true)”循环这么糟糕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用Java编程了好几年了,但我刚刚回到学校获得正式学位。我很惊讶地发现,在我的上一次任务中,我使用了如下所示的循环丢失了分数。

I've been programming in Java for several years now, but I just recently returned to school to get a formal degree. I was quite surprised to learn that, on my last assignment, I lost points for using a loop like the one below.

do{
     //get some input.
     //if the input meets my conditions, break;
     //Otherwise ask again.
} while(true)

现在我的测试我只是扫描一些控制台输入,但我被告知这种循环是不鼓励的,因为使用 break 类似于 goto ,我们只是不喜欢不要这么做。

Now for my test I'm just scanning for some console input, but I was told that this kind of loop is discouraged because using break is akin to goto, we just don't do it.

我完全理解 goto 及其Java堂兄的缺陷break:label ,我很有意识不使用它们。我也意识到一个更完整的程序会提供一些其他的逃避手段,例如刚刚结束程序,但这不是我教授引用的原因,所以...

I understand fully the pitfalls of goto and its Java cousin break:label, and I have the good sense not to use them. I also realize that a more complete program would provide some other means of escape, say for instance to just end the program, but that wasn't a reason my professor cited, so...

do-while(true)有什么问题?

推荐答案

我不会说这是 - 但同样我通常至少会寻找替代方案。

I wouldn't say it's bad - but equally I would normally at least look for an alternative.

在它是的情况下我写的第一件事,我几乎总是尝试将它重构为更清晰的东西。有时它无法帮助(或者替代方案是有一个 bool 变量除了指示循环结束之外没有任何意义,不如 break 声明)但至少值得尝试。

In situations where it's the first thing I write, I almost always at least try to refactor it into something clearer. Sometimes it can't be helped (or the alternative is to have a bool variable which does nothing meaningful except indicate the end of the loop, less clearly than a break statement) but it's worth at least trying.

作为一个更明确的使用方法的例子 break 而不是国旗,请考虑:

As an example of where it's clearer to use break than a flag, consider:

while (true)
{
    doStuffNeededAtStartOfLoop();
    int input = getSomeInput();
    if (testCondition(input))
    {
        break;
    }
    actOnInput(input);
}

现在让我们强制它使用一个标志:

Now let's force it to use a flag:

boolean running = true;
while (running)
{
    doStuffNeededAtStartOfLoop();
    int input = getSomeInput();
    if (testCondition(input))
    {
        running = false;
    }
    else
    {
        actOnInput(input);
    }
}

我认为后者更难阅读:它是得到一个额外的 else 块, actOnInput 更加缩进,如果你想弄清楚当发生什么时候会发生什么 testCondition 返回 true ,您需要仔细查看块的其余部分以检查是否存在 else 块后,无论正在运行是否已设置为 false 是否。

I view the latter as more complicated to read: it's got an extra else block, the actOnInput is more indented, and if you're trying to work out what happens when testCondition returns true, you need to look carefully through the rest of the block to check that there isn't something after the else block which would occur whether running has been set to false or not.

break 语句更清楚地传达意图,并且让剩下的块继续处理它需要做的事情,而不用担心早期的情况。

The break statement communicates the intent more clearly, and lets the rest of the block get on with what it needs to do without worrying about earlier conditions.

请注意,这与人们对多重的论点完全相同返回方法中的语句。例如,如果我可以在前几行中计算出方法的结果(例如,因为某些输入为空,或为空,或者为零),我发现直接返回该答案比使用变量存储结果更清楚,然后是一整块其他代码,最后一个返回语句。

Note that this is exactly the same sort of argument that people have about multiple return statements in a method. For example, if I can work out the result of a method within the first few lines (e.g. because some input is null, or empty, or zero) I find it clearer to return that answer directly than to have a variable to store the result, then a whole block of other code, and finally a return statement.

这篇关于是“while(true)”循环这么糟糕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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