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

查看:27
本文介绍了是“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 块,无论 running 是否设置为 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.

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

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天全站免登陆