views:

75

answers:

5

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 {
A: 

As long as you place brackets around outer if statements, you will guarantee an inner if is not correlated to an outer else:

if (X == 1) { // A
   if (Y > 1) { // B
      gothere();
  }
}

In your second example, I would add brackets around the first if to guarantee its inner if is scoped correctly:

if (skippingAvancado) {
   if (vList.length > 1)        
    changeVillage();
}
Justin Ethier
I think I could have phrased this better - but does that make sense?
Justin Ethier
+1  A: 

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

if(foo)
    if(bar)
       baz;
    else
       bazz;

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

Michael Mrozek
A: 

it's simple logic which else belongs to which if... you control it by using the correct brackets and nesting.

oezi
This is more a comment than an answer
Sean Kinsey
A: 

In your first example, C and D will not be related to B - they are elses of the first if (A). This is ensured by the braces. In the second example, the else will probably correspond to the second if (else clauses are matched to the nearest possible if), so you should add braces around

if (vList.length > 1)         
    changeVillage();

to ensure the desired behavior.

froadie
A: 

Since it sounds like you haven't fully got control of how if/else constructs work, with or without block statements I propose the following:

  • Always place the statements inside blocs {/*...*/}. This will ensure that multiple statements are grouped together correctly
  • Use proper indentation (either manually, via your IDE, or via tools like jsbeautifier.org]1 /). This will give you a visual cue as to which statements belong together. Each time you write a { you increase the indentation, each time you write a } you decrease it.

Like this:

|   |   |   |
if (foo){ // if1
    if (bar){ //if2
        ...
    } else if (foobar) { //elseif if2
        if (foobarfoo) { //if3
           ...
       } // end if3
    } // end if2
} else { //else if1
    ...
} // end if1
|   |   |   |

Failing to properly group you statements will in some browsers lead to incorrect grouping giving you a hell to debug.

Sean Kinsey