【问题标题】:C++: While loop condition executes continuously, loop body never executesC ++:循环条件连续执行时,循环体从不执行
【发布时间】:2016-03-05 18:31:06
【问题描述】:

这段代码有什么问题?为什么循环条件重复执行而循环体从不重复执行?它快把我逼疯了!我看不到问题:

bool test() { std::cout << "condition!" << std::endl; return true; }

while(test());
{
   std::cout << "This message never appears!!!" << std::endl;
}

输出:

condition!
condition!
condition!
...forever

【问题讨论】:

    标签: c++ while-loop infinite-loop


    【解决方案1】:

    我当然是盲人,原因是“while”后面的分号不对!编译器认为没有循环体,只是重复执行“无”。我读到的循环体,编译器只是认为是一个未命名/匿名的范围,在循环之后,所以它永远不会被执行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-14
      • 1970-01-01
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多