Compound IF statement scope

c

Solution

The basic syntax of `if....else` is:

              --optional-
if(expr) stmt [else stmt]

and if you just had your example minus the braces they'd nest this way:

         ---------stmt----------
if(expr) if(expr) stmt else stmt

basically meaning that the `else` gets bound with the most recent available `if`, and the syntax for the outer `if` is satisfied, since the inner `if...else` is a statement

Adding the braces gives (with the overall syntax shown again first):

if(expr)       stmt       [else stmt]

         --compound-stmt--
if(expr) { if(expr) stmt } else stmt

Here, then inner `if(expr)`..stmt is enclosed inside a compound statement (which is a subvariety of a statement), and the most recently still-open (read: in scope) `if` is the first one. You could also view the end of a compound statement - the close brace - as closing off all contained syntactic structures.

There is no `compound if`, only an `if` controlling a compound statement.

Problem

``` if()//first if { if()//second if statement; } else statement; ``` I know that `else` matches with the first `if` but my question is why?I think of it like this,first `if` and `else` are in the same scope(mains local scope for example) and the second `if` is in the first if local scope in which `else` has no visibility?Is this correct?

Original source