我已经用Java编程好几年了,但我最近才回到学校获得一个正式学位。我很惊讶地发现,在我的上一个作业中,我因为使用下面这样的循环而失分。

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

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

我完全理解goto和它的Java兄弟break:label的陷阱,而且我很明智地不使用它们。我也意识到,一个更完整的程序会提供一些其他的逃避方式,比如结束程序,但我的教授并没有提到这一点,所以……

do-while(正确)怎么了?


当前回答

如果您的循环在后台线程上运行,这可能会很糟糕,因此当您通过终止一个UI线程来关闭应用程序时,这段代码将继续执行。正如其他人已经说过的,你应该总是使用某种支票来提供一种取消的方式。

其他回答

我想没什么,真的。老师们对goto过敏,因为他们听说那里很糟糕。否则你可以这样写:

bool guard = true;
do
{
   getInput();
   if (something)
     guard = false;
} while (guard)

这几乎是一样的。

也许这样更简洁(因为所有循环信息都包含在块的顶部):

for (bool endLoop = false; !endLoop;)
{

}

也许是我运气不好。或许我只是缺乏经验。但是每次我回忆起处理while(true)内部有break时,可以改进将Extract Method应用到while-block的代码,它保留了while(true),但(巧合吗?)将所有的break转换为返回。

根据我的经验,没有休息(即返回或抛出)是相当舒服和容易理解的。


  void handleInput() {
      while (true) {
          final Input input = getSomeInput();
          if (input == null) {
              throw new BadInputException("can't handle null input");
          }
          if (input.isPoisonPill()) {
              return;
          }
          doSomething(input);
      }
  }

对我来说,问题在于可读性。

带有true条件的while语句不会告诉您任何关于循环的信息。这使得理解它的工作更加困难。

从这两个片段中更容易理解什么?

do {
  // Imagine a nice chunk of code here
} while(true);

do {
  // Imagine a nice chunk of code here
} while(price < priceAllowedForDiscount);

1) do -while没有错(正确)

2)你的老师错了。

NSFS ! !:

3)大多数老师是老师而不是程序员。

通常用于读取输入的Java约定是:

import java.io.*;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String strLine;

while ((strLine = br.readLine()) != null) {
  // do something with the line
}

通常c++读取输入的约定是:

#include <iostream>
#include <string>
std::string data;
while(std::readline(std::cin, data)) {
  // do something with the line
}

在C语言中,它是

#include <stdio.h>
char* buffer = NULL;
size_t buffer_size;
size_t size_read;
while( (size_read = getline(&buffer, &buffer_size, stdin)) != -1 ){
  // do something with the line
}
free(buffer);

或者如果你确信你知道你的文件中最长的文本行有多长,你可以这样做

#include <stdio.h>
char buffer[BUF_SIZE];
while (fgets(buffer, BUF_SIZE, stdin)) {
  //do something with the line
}

如果您正在测试用户是否输入了退出命令,那么很容易扩展这3个循环结构中的任何一个。我用Java写一下

import java.io.*;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;

while ((line = br.readLine()) != null  && !line.equals("quit") ) {
  // do something with the line
}

因此,虽然在某些情况下break或goto是合理的,但如果您所做的只是一行一行地从文件或控制台读取,那么您不应该需要while (true)循环来完成它——您的编程语言已经为您提供了使用input命令作为循环条件的适当习惯用法。