Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   else if vs else { if (http://www.velocityreviews.com/forums/t731859-else-if-vs-else-if.html)

A 08-26-2010 08:32 PM

else if vs else { if
 
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 08-26-2010 08:42 PM

Re: else if vs else { if
 
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.



Saeed Amrollahi 08-26-2010 10:00 PM

Re: else if vs else { if
 
On Aug 27, 12:32*am, "A" <a...@a.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

Ian Collins 08-26-2010 10:34 PM

Re: else if vs else { if
 
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

A 08-27-2010 10:36 AM

Re: else if vs 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



Ian Collins 08-27-2010 10:50 AM

Re: else if vs else { if
 
On 08/27/10 10:36 PM, A wrote:

Please don't snip attributions!

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


Read with care, elseif != else if

--
Ian Collins

Felix Palmen 08-27-2010 10:56 AM

Re: else if vs else { if
 
* A <a@a.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 <felix@palmen-it.de>
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

Gert-Jan de Vos 08-27-2010 11:36 AM

Re: else if vs else { if
 
On Aug 26, 10:32*pm, "A" <a...@a.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?

James Kanze 08-28-2010 08:55 PM

Re: else if vs else { if
 
On Aug 27, 11:36 am, "A" <a...@a.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


All times are GMT. The time now is 02:23 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.