微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用箭头代码重构无限循环

如何解决用箭头代码重构无限循环

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

// 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;

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。