Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用Arrow码重构无限循环_C++_C++11_If Statement - Fatal编程技术网

C++ 用Arrow码重构无限循环

C++ 用Arrow码重构无限循环,c++,c++11,if-statement,C++,C++11,If Statement,我有一个具有特定周期(延迟)的无限循环。我的结构如下: // define the i32threshold of time // set i32counter to 0 while(true) { if(bExecuteEnable) { if(bConnectionSet) { if(++i32counter >= i32threshold) { // Do the job counter = 0;

我有一个具有特定周期(延迟)的无限循环。我的结构如下:

// define the i32threshold of time
// set i32counter to 0
while(true)
{
  if(bExecuteEnable)
  {
    if(bConnectionSet)
    {
      if(++i32counter >= i32threshold)
      {
        // Do the job
        counter = 0;
      }
    }
  }

  delay(1); // 1 ms of delay introduces the period
}

除了将所有条件合并到单个
if
语句中之外,您是否有任何建议来重构这个无限循环?

重写它看起来简单得多:

while (!bExecuteEnable && !bConnectionSet && ++i32counter < i32threshold) {
  delay(1); // 1 ms of delay introduces the period
}

counter = 0;
while(!bExecuteEnable&&!bConnectionSet&&++i32计数器

当然希望
i32threshold
小于
i32counter
的最大值,否则您将永远溢出并循环。

循环本身有什么问题?
延迟(i32threshold)可能会消除一种情况,如前所述。@AlanBirtles,它可以工作,但4个巢穴看起来太像箭头。”。我认为它可以改变。是的,它可以,你可以使用一个if语句,正如你已经说过的,这有什么问题吗?@AlanBirtles这没关系,但我想知道是否还有其他我不知道的练习。