Just a quick qeustion:D
is their a prefered time to use a series of if...else statments against the use of a series of just 'if' statements?
Printable View
Just a quick qeustion:D
is their a prefered time to use a series of if...else statments against the use of a series of just 'if' statements?
well if you think about it else if is the same as doing this
its probably better to use a switch rather than multiple if'sCode:else
{
if(blah)
{
//code
}
}
hmm.. id ont' think your seeing what i'm trying to say. You can't use switch sometimes because switch statements don't handle relational operators. so just pretend switch is out of the question.
Is there a time where using else...if's is better than using a series of if's? Or does it matter?
it depends on what you are coding and what you want the program to do.
use multiple ifs if they are right for the situation or use else clauses if they are right. There is no right or wrong. You will soon see when you write code that there are uses for both constructs.
Remember that else introduces a second choice.
your basically choosing between
if(something) choice 1
or
if (something) choice 1
else choice2
If I read correctly your posts, take this example:
in thid piece of code your program checks every single event. But the event you are testing for is mutially excluse. Two of those things can't happen at the same time. With this:Code:if(var == 1) do this;
if(var == 2) do that;
if(var == 3) do this other thing;
in this one, as soon as your app finds the correct code, it jumps to the next code it'll have to execute.Code:if(var == 1) do this;
else if(var == 2) do that;
else if(var ==3) this other thing;
In the first one, you lose time checking for a situation you know can't happen. In the second one your program will execute faster because it doesn't lose time checking for something that can't happen. (i think :))
Hope this helps