else if vs else { if

Discussion in 'C++' started by A, Aug 26, 2010.

  1. A

    A Guest

    Is there any difference between:

    if (a == '1')
    {
    // do something
    }
    else if (b == '2')
    {
    // Here a is supposed to be != '1'
    // do something if a != '1' and b == '2'
    }


    .........and this..........


    if (a == '1')
    {
    // do something
    }
    else
    {
    // Here a != '1'
    if (b == '2')
    {
    // do something if a != '1' and b == '2'
    }
    }

    as some languages (php) make difference between "else if" and "else { if"...
    A, Aug 26, 2010
    #1
    1. Advertising

  2. A

    A Guest

    I'm guessing the answer will be - if there is no standalone else then "else
    if" is the same like "else { if", just wanted to check.
    A, Aug 26, 2010
    #2
    1. Advertising

  3. On Aug 27, 12:32 am, "A" <> wrote:
    > Is there any difference between:
    >
    > if (a == '1')
    >     {
    >     // do something
    >     }
    > else if (b == '2')
    >     {
    >     // Here a is supposed to be != '1'
    >     // do something if a != '1' and b == '2'
    >     }
    >
    > ........and this..........
    >
    > if (a == '1')
    >     {
    >     // do something
    >     }
    > else
    >     {
    >     // Here a != '1'
    >     if (b == '2')
    >         {
    >         // do something if a != '1' and b == '2'
    >         }
    >     }
    >
    > as some languages (php) make difference between "else if" and "else { if"....


    Hi

    I tested both codes and there is no difference.
    there are a couple of items, I like to add:
    1. I usually use the if ... else statement for testing one condition,
    I mean, testing one expression against some values like:
    if (a == '1') {
    // do something
    }
    else if (a == '2') {
    // do something
    }
    // ...
    2. In the 2nd if .. else statement, you don't need to a block.
    Because you have just an if statement. You can write:
    if (a == '1')
    {
    // do something
    }
    else
    // Here a != '1'
    if (b == '2')
    {
    // do something if a != '1' and b == '2'
    }

    Regards,
    -- Saeed Amrollahi
    Saeed Amrollahi, Aug 26, 2010
    #3
  4. A

    Ian Collins Guest

    On 08/27/10 08:32 AM, A wrote:
    >
    > as some languages (php) make difference between "else if" and "else { if"...


    Do they? Is so, how?

    --
    Ian Collins
    Ian Collins, Aug 26, 2010
    #4
  5. A

    A Guest

    A, Aug 27, 2010
    #5
  6. A

    Ian Collins Guest

    Ian Collins, Aug 27, 2010
    #6
  7. A

    Felix Palmen Guest

    * A <>:
    >> Do they? Is so, how?

    >
    > what i meant to say is that there is a difference. take a look -
    > http://php.net/manual/en/control-structures.elseif.php


    This is purely cosmetic and my assumption is that PHP introduced an
    elseif statement just to support their weird alternative if-then-else
    syntax properly.

    I'd stick to "else if" and blocks in curly braces in any language
    supporting this (C, C++, php, perl, ...?) for best readability. But
    there is no difference to other possible notations.

    Regards, Felix

    --
    Felix Palmen (Zirias) + [PGP] Felix Palmen <>
    web: http://palmen-it.de/ | http://palmen-it.de/pub.txt
    my open source projects: | Fingerprint: ED9B 62D0 BE39 32F9 2488
    http://palmen-it.de/?pg=pro + 5D0C 8177 9D80 5ECF F683
    Felix Palmen, Aug 27, 2010
    #7
  8. On Aug 26, 10:32 pm, "A" <> wrote:
    > Is there any difference between:
    >
    > if (a == '1')
    >     {
    >     // do something
    >     }
    > else if (b == '2')
    >     {
    >     // Here a is supposed to be != '1'
    >     // do something if a != '1' and b == '2'
    >     }
    >
    > ........and this..........
    >
    > if (a == '1')
    >     {
    >     // do something
    >     }
    > else
    >     {
    >     // Here a != '1'
    >     if (b == '2')
    >         {
    >         // do something if a != '1' and b == '2'
    >         }
    >     }
    >
    > as some languages (php) make difference between "else if" and "else { if"....


    The difference is cosmetic. When you need more then 2 if's:

    With the first style you just append the additional else if () blocks
    at the
    same logical indent level.

    With the second style you nest any additional if () else inside the
    previous
    else block.

    Both are semantically equivalent but convey a different message to the
    reader: are all ifs equivalent or is the ordering/nesting significant?
    Gert-Jan de Vos, Aug 27, 2010
    #8
  9. A

    James Kanze Guest

    On Aug 27, 11:36 am, "A" <> wrote:

    [Concerning the difference between:

    if (...) {
    ...
    } else if (...) {
    ...
    }

    and

    if (...) {
    ...
    } else {
    if (...) {
    }
    }

    > > Do they? Is so, how?


    > what i meant to say is that there is a difference. take a look
    > -http://php.net/manual/en/control-structures.elseif.php


    Some other languages do have a special token, elseif (or elsif,
    or elif). In the ones I know, if and else also automatically
    open a block; the equivalent of C/C++'s opening brace is
    implicitly present after the else. So you need a special token
    in order to use the first form above, which is, obviously, the
    preferred form in all languages.

    --
    James Kanze
    James Kanze, Aug 28, 2010
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. piyush
    Replies:
    0
    Views:
    1,839
    piyush
    Jul 14, 2004
  2. Kelvin
    Replies:
    1
    Views:
    3,130
    Kelvin
    Sep 13, 2003
  3. Replies:
    25
    Views:
    650
    MonkeeSage
    Oct 1, 2006
  4. kj
    Replies:
    15
    Views:
    540
    Lawrence D'Oliveiro
    May 23, 2009
  5. Gergely Kontra
    Replies:
    16
    Views:
    223
    Devin Mullins
    Sep 20, 2005
Loading...

Share This Page