Quote:
Originally Posted by -Ever-
I'm new to programming, but I think using an ELSE within the If/End If is the right way to go.
|
its a better way to go for sure. the problem with the sequence of
if statements is that each and every
if has to be evaluated, then the variable is assigned, and the next
if is evaluated. if the evaluation fails, the variable is not set, but even if it succeeds the next
if statement is evaluated.
with an
if/else with two conditions in the
if statement (ie
if(grade <= 100 && grade >= 90){
return 'A';
}
elseif(grade < 90 && grade >= 80){
return 'B';
}
with most compilers and interpreters the
if will stop the evaluation on the first fail. It knows that if the first condition fails, an AND is impossible so it kicks out. In a sequence of
if/else statements, once one matches, it kicks out of the sequence.
a compiler does a good job of optimizing code if you let it.