else if vs else { if

A

A

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

A

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.
 
S

Saeed Amrollahi

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
 
F

Felix Palmen

* A said:
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
 
G

Gert-Jan de Vos

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?
 
J

James Kanze

[Concerning the difference between:

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

and

if (...) {
...
} else {
if (...) {
}
}
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top