I am new to C++. I often see conditional statement like below:
if
statement_0;
else if
statement_1;
Question:
Syntacticall
As already answered, it isn't. They are two keywords. It's start of two statements one following each one other. To try make it a bit more clear, here's the BNF gramar which deal with if
and else
statements in C++ language.
statement:
labeled-statement
attribute-specifier-seqopt expression-statement
attribute-specifier-seqopt compound-statement
attribute-specifier-seqopt selection-statement
attribute-specifier-seqopt iteration-statement
attribute-specifier-seqopt jump-statement
declaration-statement
attribute-specifier-seqopt try-block
selection-statement:
if ( condition ) statement
if ( condition ) statement else statement
Note that statement
itself include selection-statement
. So, combinations like:
if (cond1)
stat
else if(cond2)
stat
else
stat
are possible and valid according to C++ standard/semantics.
Note: C++ grammar take from this page.