Here is the example fragment properly indented.
Matching ifs and elses have matching colors.
if ( a == b ) if ( d == e ) total = 0; else total = total + a; else total = total + b;
Sometimes you must use braces { and }
to say what you want.
An else inside of a pair of braces
must match an if also inside that pair.
Sometimes you don't need braces,
but use them
to make clear (to human readers) what you intend.
Here is the complete rule for matching
ifs and elses:
Rule for Matching if and else: Within each pair of matching braces: Start with the firstifand scan downward. Eachelsematches the first unmatchedifabove it. Anifmatches only oneelseand anelsematches only oneif. Someifs will not have a mathcingelse, but eachelsewill have a matchingif.
Here is a program fragment that uses braces.
The blue if matches the blue else.
The braces are needed to make this so.
if ( ch == 'q' ) { if ( sum == 12 ) ch = 'b' ; } else ch = 'x' ;
Match the ifs and elses in the following:
if ( a == b ) { if ( d == e ) total = 0; else total = total + a; }