Selection Statements
Three types of selection statements exist in C:
if
(
expression )
statement
In this type of if-statement, the sub-statement will only be executed iff the expression is non-zero.
if
(
expression )
statement else
statement
In this type of if-statement, the first sub-statement will only be executed iff the expression is non-zero; otherwise, the second sub-statement will be executed. Each else
matches up with the closest unmatched if
, so that the following two snippets of code are not equal:
if(expression)
if(secondexpression)statement1;
else
statement2;
if(expression)
{
if(secondexpression)statement1;
}
else
statement2;
because in the first, the else
statement matches up with the if statement that has secondexpression
for a control, but in the second, the braces force the else
to match up with the if that has expression
for a control.
If ...... else if ..... else if ..... else