So does "else if" enter a new scope?

This is doesn't matter, but I'm curious.

Does else if behave like this, being a combination of an else + if keyword
1
2
3
4
5
6
7
8
  if(a==1){
    //blah
  }else
  {if(b==1)
    {
      //blah
    }
  }


Or is "else if" a keyword itself?
That's how i understand it too yes. But i'm also a beginner so what do i know :).

Only "else" would always continue if the "if" is false.

Only "if" would always read.

"else if" would only read if the previous "if" doesn't read and the "else if" is true.


Like you have in your code.
Last edited on
"else if" is essentially a keyword it's self. It does not enter a new scope, though the code in your example IS valid. What your code equates to is... If a == 1, do action x. If a != 1, then... (If b == 1, do action y)
What else if does, is...
If a==1, action x. If a!=1 AND b==1, action y.
"else if" can be compared to a switch statement, but expanded. Once one condition is met, the rest are skipped.
if or else if by themselves don't create a new scope. The braces at lines 1/3 and 5/7 each create a scope. So in each case, // blah is in a separate scope.
Last edited on
Topic archived. No new replies allowed.