Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > else if vs else { if

Reply
Thread Tools

else if vs else { if

 
 
A
Guest
Posts: n/a
 
      08-26-2010
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"...


 
Reply With Quote
 
 
 
 
A
Guest
Posts: n/a
 
      08-26-2010
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.


 
Reply With Quote
 
 
 
 
Saeed Amrollahi
Guest
Posts: n/a
 
      08-26-2010
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
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-26-2010
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
 
Reply With Quote
 
A
Guest
Posts: n/a
 
      08-27-2010
> 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


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      08-27-2010
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
 
Reply With Quote
 
Felix Palmen
Guest
Posts: n/a
 
      08-27-2010
* 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
 
Reply With Quote
 
Gert-Jan de Vos
Guest
Posts: n/a
 
      08-27-2010
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?
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      08-28-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What's the use of the else in try/except/else? kj Python 15 05-23-2009 02:18 AM
for: else: - any practical uses for the else clause? metaperl.etc@gmail.com Python 25 09-30-2006 11:01 PM
ruby idiom for python's for/else while/else Gergely Kontra Ruby 16 09-20-2005 08:35 PM
Does no one else think microsoft does a poor job? =?Utf-8?B?SmVyZW15IEx1bmRncmVu?= Wireless Networking 2 11-20-2004 12:17 AM
Wireless, what else? HelpPls Wireless Networking 2 07-14-2004 01:30 AM



Advertisments