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这没关系,但我想知道是否还有其他我不知道的练习。