Introduction ==decision making and
branching
vC language possesses such decision
and branching by supporting likes that statement…
1. If statement.
2. Switch statement.
3. Conditional operator statement.
4. Go to statement.
Dangling else problem:
One
of the classic problem encountered when we start using nested if …else
statement is the dangling. This happen when a matching else is not available
for an if.
This
answer to this problem is very simple. Always match an else to the most
recent unmatched if in the current block. In some case it is possible that
the false condition is not required. In such solution else statement may be
omitted
“Else
is always paired with the most recent unpaired if”.
|
vRules for indentation.
When using
control statement often controls may be many other statements that must
follow it.
Ø Indent statements that are dependent on the
previous statement; provide at least three space of indentation.
Ø Align vertically else cause with their matching if
clause.
Ø Indent the statement is the block by at least
three spaces to the right of the brace.
Ø Align the opening and closing brace.
Ø Use appropriate comments to signify the beginning
and the end of blocks.
Ø Indent the nested statement as per above value.
Ø Code only one clause or statement on each line.
|
vRules for switch statement
· The switch expression must be an
integer type.
· Case labels must be in constant
or constant expression.
· Case labels must in unique. No
two labels can have the same values.
· Case labels must be end with a
semicolon.
· The break statement transfers the control
out of the switch statement.
· The break statement is optional. That is two
or more case labels may belong to the same statement.
· The default labels are optional.
If present it will executed when the expression does not find matching the
case.
· There can be at most one default labels.
· It is permitted to nest switch
statement.
|
Over all just remember.
Ø Be aware of dangling else
statement.
Ø
No comments:
Post a Comment