Just a quick qeustion
is their a prefered time to use a series of if...else statments against the use of a series of just 'if' statements?
Just a quick qeustion
is their a prefered time to use a series of if...else statments against the use of a series of just 'if' statements?
#include <Jesus.h>
It will save your life
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 } }
guns dont kill people, abortion clinics kill people.
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?
#include <Jesus.h>
It will save your life
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
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
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
SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
I say what I say, I mean what I mean.
IDE: emacs + make + gcc and proud of it.