while...else Arduino 语句

Dj-*_*awa 3 arduino arduino-ide

是否有可能在 Arduino 中的 while 循环之后添加 else,就像 Java 或 C# 中那样?像这样的东西:

while(condition){
  doThingA;
} else{
  doThingB;
}
Run Code Online (Sandbox Code Playgroud)

Paw*_*wel 5

C# 没有while...else,我认为 Java 也没有这个结构。elsePython 拥有它,并且因为只有当您从循环中断时才执行块中的指令,因此您可以按如下方式模拟它:

bool flag = TRUE;
while (condition)
{
    if (anothercondition)
    {
       flag = FALSE;
       break;
    }
}

if (flag)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)