If I have a statement block like this:
if (/*condition here*/){ }
else{ }
or like this:
if (/*condition here*/)
else if (/
Many languages have a grammer like this (here: ECMAScript Language Specification, so JavaScript):
IfStatement :
if (Expression)StatementelseStatement
if (Expression)StatementStatement :
Block
VariableStatement
EmptyStatement
ExpressionStatement
IfStatement
IterationStatement
ContinueStatement
BreakStatement
ReturnStatement
WithStatement
LabelledStatement
SwitchStatement
ThrowStatement
TryStatementBlock :
{StatementListopt}StatementList :
Statement
StatementList Statement
So the branches of an ifStatement may contain a block of statements (Block) or one of the other statements (other than Block). That means this is valid:
if (expr)
someStatement;
else
otherStatement;
And as StatementList may just contain a single statement, these examples are equivalent to the previous:
if (expr) {
someStatement;
} else {
otherStatement;
}
if (expr)
someStatement;
else {
otherStatement;
}
if (expr) {
someStatement;
} else
otherStatement;
And when we replace otherStatement by an additional IfStatement, we get this:
if (expr) {
someStatement;
} else
if (expr) {
someOtherStatement;
}
The rest is just code formatting:
if (expr) {
someStatement;
} else if (expr) {
someOtherStatement;
}