Friday, December 13, 2013

if v.s. else if

I am always confused about when to use a bunch of  "if", when to use "else if".

For "a bunch of if" I mean:
if(cond1){}
if(cond2){}
if(cond3){}

For "else if" I mean:
if(cond1){}
else if(cond2){}
else if(cond3){}
else if(cond4){}


When you want the conditions to be mutually exclusive, use "else if";
if not, you can use a bunch of "if".

Following this rule, it will be easier to "think-it-through" when debugging.

No comments:

Post a Comment