我在一个类似下面的函数中有一个 do-while 循环:
do
{
// a bunch of stuff
if (something < something else)
{
return true;
}
else if (stuff > other stuff)
{
if (something != other stuff)
{
return false;
}
else
{
return true;
}
}
} while (condition);
我的问题是最后的 condition
。我能跟踪的唯一方法是在循环之前声明一个 bool 变量,并将其值设置为匹配 return
值并让 while()
在每次迭代后检查它。虽然这可行,但对我来说似乎相当不雅,我想知道是否有一种方法可以让 while()
改为使用 return
值。
最佳答案
目前尚不清楚您的情况。不管怎样,你可能想要一个无限循环:
for (; ;) {
… your code here …
}
或:
while (true) {
… your code here …
}
这个循环永远不会自行停止......但是因为你使用 return
退出它,所以这不是问题。
关于c++ - do-while 条件而不声明单独的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5353792/