3

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;
}
Dj-Wawa
  • 31
  • 1
  • 1
  • 3

4 Answers4

4

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)
{
    ...
}
Pawel
  • 31,342
  • 4
  • 73
  • 104
0

No maybe, it's not actually because it is based on embedded c, which includes syntaxes similar to c. So to conclude it simply can't. was a pleasure to help you.

-1
ESPserial.print(params + '\n');

time = millis();
while(true) {
    int avail = ESPserial.available();

    if (avail) break;
    if (millis() - time > 500) break;
}

do {
    char ch = ESPserial.read();
    data += ch;
} while (ESPserial.available());
-1

You can't have a else after a whole loop but u can put a exclamation(!) before the statement like:

while (!condition) orwhile(value1 != value2)

xbox gamer
  • 101
  • 3