A
problem in some
programming languages where an
else doesn't
match the
if it's intended to match.
if (x >= 0)
if (x >= 100)
x = x-100;
else
return -1;
doSomethingElse (x);
The
indentation would lead one to think that this
code checks to see if x is greater than zero, if it is, checks again to see if it's over 100, if it is, subtract 100 if x was
negative to begin with, return, and then do something to x. However, despite the indentation, that else associates with the second if, always returning if x is less than 100. The
solution to the
dangling else is the use of
braces (or
begin/
end or
whatever) to enforce the correct association.
if (x >=0) {
if (x >= 100) {
x = x - 100;
}
}
else {
return -1;
}
doSomethingElse (x);
It's a
Good Thing to use braces even when not needed to avoid this and other problems when you
modify your code.