Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

while...else Statement for Arduino

Is there a possibility to add an else after a while-Loop in Arduino, like there is in Java or C#? Something like this:

while(condition){
  doThingA;
} else{
  doThingB;
}
like image 584
Dj-Wawa Avatar asked Apr 12 '26 16:04

Dj-Wawa


1 Answers

C# does not have while...else and I don't think Java has this construct either. Python has it and because the instructions in the else block are not executed only when you break from the loop you can emulate it as follows:

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

if (flag)
{
    ...
}
like image 142
Pawel Avatar answered Apr 25 '26 23:04

Pawel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!