What line is the error in? Web IDE and Spark Dev don't show

error: expected unqualified-id before ‘else’

here is a complete test code, remove ### line and you can compile:

void setup()
{
}

void loop() {

    if(false)
    {
    }
     else //### here is the error
    if(true) 
    {
    }
    else
    {
    }
}

What is going on there?

Found this in another context but it works as well:

#pragma SPARK_NO_PREPROCESSOR

Adding this makes the code compile. Not sure why this is a preprocessor issue.

1 Like

When you wrap your else block in a pair of { } it compiles too.

But it should work without the braces just the same. Maybe this is something @Dave would like to look at, too.

// this compiles fine
    if(false)
    {
    }
    else 
    {
      if(true) 
      {
      }
      else
      {
      }
    }

// this does not
    if(false)
    {
    }
    else if(true) 
    {
    }
    else
    {
    }

I had similar problem in both Spark IDE (compiling from commandline in Spark cloud using spark-cli) and plain Arduino IDE. Something like this didn’t work:

bool a;
bool b;

if (a) {
    ...
} else if (b) {
    ...
}

This fixed the problem:

if (a == true) {
    ...
} else if (b == true) {
    ...
}

Edit: updated to mention the code had same bug on Spark, too

1 Like