expected unqualified-id before 'else'... on a line where there is no 'else' !!

Dear Arduino experts,

I have problems with my (quite large) program... After the last round of modifications and additions, it doesn't compile any more.
It returns the following error:

137: error: expected unqualified-id before 'else'

Problems are:

  • the file is the top level of my project, but has not been changed
  • it contains mostly pins setup and the main loop
  • there is no "else" near line 137 (we are in the middle of input/output pins setup)
  • if I add an instruction like: i=0; in line 137 or 136, I get the same error at the same line

I have compared 2 versions before and after the error appeared, and could not find anything that looks suspicious (I added a new function, deleted/added a few things, but I did not find any obvious structural mistake).

Any idea?
Any good tool to check my program in another environment than the Arduino IDE?

Thanks !!

Do a CTRL-T, see if you have mismatched number of ( )s or { }, that is usually the culprit. Or a ; got deleted somewhere, or you're missing a , in a list of elements in an array. It will be some kind of typo like that.

Funny thing... When I comment-out my new function in another .ino file, the error goes away !!
Looks like error messages are not always helpful!!! :grin:

BTW, the structure of the function looks OK... just this nested if/else that fails when I un-comment the 'else if' portion!!

  if (...) {
    ...
    if (...) {
      ...
    } else if (...) {
      ...
    }
  } else {
    ...
  }

Does Arduino have a problem handling this??

BTW, the function is a bit more complex than that, with nested if's in place of the ... in the code above

Thanks for your suggestion, CrossRoads.

Indeed if I change my code to this, it works!!!

  if (...) {
    ...
    if (...) {
      ...
    } else {
      if (...) {
        ...
      }
    }
  } else {
    ...
  }

Conclusion: WARNING about compiler error messages!! They don't tell you the truth! :grin:

if (...) {
...
if (...) {
...
}
} <<< I think you are missing these

else { <<< I think you are missing these

if {
...
}

}

else {
...
}

If not quite that, something about the nesting is not quite right.
Ah, I see you got it figured out.

Yes thanks, it compiles now, but the structure was correct...
Just an else if that I had to split. Strange? :roll_eyes:

Not at all. I would say the structure was not complete - the missing braces define the logic, and it wasn't quite correct.

This is one reason I'm a fan of always having curly braces on new lines, and all [if then else] code indented so you can match it all up vertically.

 if (...)
{
  ...
  if (...)
  {
    ...
  } 
  else if (...)
  {
    ...
  }
}
else
{
  ...
}

Let's necro this thread, maybe it helps somebody. Encountered same problem and braces structure WAS correct, still was complaining about "else if". Spent an hour on it and finally found out boolean was the culprit.

bool var1 = true;
bool var2 = true;

// This doesn't compile:
if (var1) {
    // ...
} else if (var2) {
    // ...
}

// This does:
if (true == var1) {
    // ...
} else if (true == var2) {
    // ...
}

So a boolean value itself doesn't work, but comparison does. Well, I use mostly high level languages now, but I can't remember C would not allow for standalone boolean expression in else if (...). Am I just stupid? Or what trickery is this?

Compiles fine for me (1.0.4). If you're still interested in chasing this down, best post the whole sketch that fails.

@Lami you are a genius. Bool comparisons work, bool alone fails. Weirdness.