Thread: else if...

  1. #1
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942

    else if...

    Is it better to use:

    Code:
    if(stuff)
    {
        stuff
    }
    else
    {
         if(stuff)
         {
             stuff
         }
         else
         {
              stuff
         }
    }
    or:
    Code:
    if(stuff)
    {
        stuff
    }
    break;
    if(stuff)
    {
        stuff
    }
    else
    {
        stuff
    }
    I'm not sure if I got that right, but whatever.

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    the first one since break is used to exit a loop.

    Code:
    while(1)
    {
          if(...)
             break;
    
          ....
    }

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: else if...

    Originally posted by gcn_zelda
    Is it better to use:

    Code:
    if(stuff)
    {
        stuff
    }
    else
    {
         if(stuff)
         {
             stuff
         }
         else
         {
              stuff
         }
    }
    or:
    Code:
    if(stuff)
    {
        stuff
    }
    break;
    if(stuff)
    {
        stuff
    }
    else
    {
        stuff
    }
    I'm not sure if I got that right, but whatever.
    Your two pieces of code do completely different things. The first one won't even work outside of a loop or switch. Also, in your first one, if you are in a loop, anything after the first if statement will never be executed because no matter what, the break statement will be reached. You most-likely meant to put the break inside your first if statement. Post the rest of your code and then we can say which you should use (or provide another solution).

  4. #4
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    Yeah. I was just wondering. I haven't written the code yet.
    I see what I did wrong.

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Code:
    if(stuff)
    {
        stuff
    }
    else if(stuff)
    {
         stuff
    }
    else
    {
         stuff
    }
    This is the same logically as your first one, but in my opinion is more readable.

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    like everybody else said, the break; won't compile unless it's in a loop... and what's under it won't happen because it breaks out of the loop before it does... for readability, I would set it up like this:
    Code:
    if(true)
         doSomething
    
    if(SomeThingElseIstrue)
         DoSomethingElse
    else
         DoSomethingDifferent
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    I prefer a minor change to jlou's idea:
    Code:
    if(stuff)
    {
        stuff
    }
    else 
    if(stuff)
    {
         stuff
    }
    else
    {
         stuff
    }
    major_small's idea simply removes the first else, which is probably not a good idea.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Originally posted by WaltP
    Code:
    else 
    if(stuff)
    Eewww, gross!

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Code:
    if(cond1) ...
    else if(cond2) expr2
    else ...
    and
    Code:
    if(cond1) ...
    if(cond2) expr2
    else expr3
    Are potentially different depending on your conditions, so make sure you know which one you intend to use. The first will only execute expr2 if cond1==false and cond2==true, and expr3 if both cond1 and cond2 are false. The second will execute expr2 if cond2==true regardless of what cond1 is, and similarly, the execution of expr3 is dependent only on the truth value of cond2.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by jlou
    Eewww, gross!
    To each his own, I guess.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Originally posted by WaltP
    major_small's idea simply removes the first else, which is probably not a good idea.
    actually, jlou's idea puts it in, which is a bad idea... check the original code

    what jlou did could make the program skip a check that the programmer wanted done, for example:
    Code:
    if(foo==true)
         do that;
    else if(bar==true) //skipped if foo is true
         do nothing;
    else
         do this;
    that doesn't take into account that in this run, both foo and bar are true and the program is supposed to do this and that
    Last edited by major_small; 01-01-2004 at 10:24 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by major_small
    actually, jlou's idea puts it in, which is a bad idea... check the original code
    Just did. Original post was:
    Code:
    if(stuff)
    {
        stuff
    }
    else
    {
         if(stuff)
         {
             stuff
         }
         else
         {
              stuff
         }
    }
    Originally posted by major_small
    what jlou did could make the program skip a check that the programmer wanted done...
    No, what jlou did was reformat the code to:
    Code:
    if(stuff)
    {
        stuff
    }
    else if(stuff)
    {
         stuff
    }
    else
    {
         stuff
    }
    All he really did was change the indentation.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  13. #13
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    oh I see... I read the second piece of code, which was meant to do the same thing as the first piece of code... sorry.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed