is it any serious difference between if else and else if

is it any serious difference between if else and else if

if else () {
} 

else if () {
}

Yes
Read:
https://www.arduino.cc/en/Reference/Else

Using 'else if' is similar to switch/case

I see no real difference, both snippets will not compile, so they are functional identical.

No, else if () won't compile as there's no if() before the else.

if else () {} won't compile and even if it did, won't do what you expect either. The code in the braces {} will always be executed because the expression in brackets () forms the body of the else clause.

thanks

The only two valid forms for an if-statement are:

 if (EXPRESSION) STATEMENT

or

  if (EXPRESSION) STATEMENT else STATEMENT

Your "if else () S{}" won't compile because there is no '(' after the 'if'.

Your "else if () {}" won't compile on its own because the 'else' does not follow " if (EXPRESSION) STATEMENT" and because the '()' after your 'if' do not contain an EXPRESSION.

To elaborate a little on JohnWasser's reply #5

else if is nothing more than

if (firstCondition == true)
{
}
else
  if (secondCondition == true)
  {
  }
  else
    if (thirdCondition == true)
    {
    }

Note that (in my attempt to make it clear) I left out the {} around the elses; not something I would do under normal circumstances.

OP: it's pretty obvious that you asked this question without even typing the code into the IDE and seeing if it compiles.

As always when someone asks a really, really basic question about C++ syntax, I suggest you google "C++ tutorial" and do the first few chapters of any of the seveal free online tutorials on the web. Personally, I learned C out of the blue book in '85 using 'ed' on a teletype machine to edit my code without having to go to forums to ask people questions like this.

Grrrr.

PaulMurrayCbr:
Personally, I learned C out of the blue book in '85 using 'ed' on a teletype machine to edit my code without having to go to forums to ask people questions like this.

Grrrr.

I was a little later; wrote my first serious C program (for my thesis on digital image processing) in 1988 using vi as the editor :slight_smile: Thank goodness I had a mentor that was a quite well versed in C.

As others have pointed out

if else ()

is meaningless

However, compare these two and note their difference

if (this is true)  //single condition
  {
    do this if the condition is true
  }
else
  {
    always do this
  }

and

if (this is true)  //first condition
  {
    do this if the first condition is true
  }
else if (this is true) //second condition
  {
    do this if the second condition is true
  }

Note the implied

else
  {
    do nothing
  }

at the end of the second example

NOTE ********************************
See subsequent posts for corrections to this one

UKHeliBob:

if (this is true)  //single condition

{
   do this if the condition is true
 }
else
 {
   always do this
 }

Don't you mean:

if (this is true)  //single condition
 {
   // do this, since the condition is true
 }
else
 {
   // don't do this, since the the condition is true
 }

Don't you mean:

Whoops on my part.
Thanks for spotting and pointing that out.

Rather than cause confusion I will leave my original (wrong) post in place.