if or else...if?

This is a discussion on if or else...if? within the C++ Programming forums, part of the General Programming Boards category; Just a quick qeustion is their a prefered time to use a series of if...else statments against the use of ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    38

    Question if or else...if?

    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

  2. #2
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    well if you think about it else if is the same as doing this
    Code:
    else
    {
         if(blah)
          {
             //code
           }
    }
    its probably better to use a switch rather than multiple if's
    guns dont kill people, abortion clinics kill people.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    38
    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

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    it depends on what you are coding and what you want the program to do.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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

  6. #6
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    If I read correctly your posts, take this example:
    Code:
    if(var == 1) do this;
    if(var == 2) do that;
    if(var == 3) do this other thing;
    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;
    else if(var == 2) do that;
    else if(var ==3) 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.
    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.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21