How to control to which if the else is correlated?
ex:
if X = 1 { // A
if Y > 1 { // B
gothere();
}
}
else if X < 1 { // C
gohere();
}
else { // D
gonowhere();
}
how to make sure C and D will not be related to B???
here´s another example:
if xxx {
...
FM_log(7,"vList.length = "+vList.length);
if (skippingAvancado)
if (vList.length > 1)
changeVillage();
else {
if (skipcounter >= maxSkipCount) {
FM_log(7,"ROTINA ANTIGA SKIPCOUNTER");
changeVillage();
}
}
else {
This is the dangling else problem; it’s ambiguous and the language can choose either construct to bind to. Most (if not all) languages group the else with the nearest if, so it would be
In practice you should always use braces to make sure there’s no ambiguity, as you did in your first example. If you took the braces out of the first example, C and D would be attached to B instead of A; since you included them, the blocks work as you wanted