Archived

This topic is now archived and is closed to further replies.

iwasbiggs

point of 'else if' ?

Recommended Posts

Just wondering if 'else if' in C++ was really considered a separate keyword. I know that MASM has an .ELSEIF test, but as far as I can tell, there isn't really a difference from a regular else then an if test. i.e.
        
if()
{
}
else
{
   if()
   {
   }
}
        
Edited by - iwasbiggs on 6/16/00 11:58:59 PM

Share this post


Link to post
Share on other sites
I really don''t understand you much, but the elseif statement is for if''s with more than 2 possibilities.

Also, in C/C++, isn''t there an actual elseif statement...like the following

if SomethingSomething
{
}
elseif somethingelse
{
}

------------------------
Captured Reality.

Share this post


Link to post
Share on other sites
No, there''s no ''elseif'' keyword in C/C++. You can just do it like this though:

if(x == 10)
{
}
else if(x == 5)
{
}

Although that''s just a different way of formatting the if statements...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
AFAIK in C, when the compiler reaches a { symbol it means the start of a block. Blocks are needed to group statements together to make a few statements count as one.

so...


if (x == 1)
{
// Do something
}
else if (y == 2)
{
// Do something else
}


is exactly the same as


if (x == 1)
{
// Do something
}
else
{
if (y == 2)
{
// Do something else
}
}


The only difference is that in the first case, the "if" statement isn''t inside a block. It doesn''t need to be, since an "if" counts as one statement, so you don''t need to enclose it in {}s.

Man this is a pointless post... :-D

CYAS!

Share this post


Link to post
Share on other sites
I disagree with all of you
else if is for multiple else ifs
what I mean is this: if you use else{ if{ you can't use else if again. or at least it won't do anything let me demonstrate

if (a == 1)
{
stuff;
}
else
{
if (a == 2)
{
more.stuff;
}
} // this is as many if statements as you can have (w/o nesting them


so instead we use this

if (a == 1)
{
stuff;
}
else if (a == 2)
{
more.stuff;
}
else if (a == 3)
{
even.more.stuff;
}


see?
hope this made sense
basically else if statements can make your code more like switches and they are more efficient than using multiple consecutive if statements for the same thing, because you aren't double testing

later
arsenius


after three days without programming, life becomes meaningless

Edited by - arsenius on June 18, 2000 5:03:25 AM

Share this post


Link to post
Share on other sites
slight as it may be.....else if statements also improve speed of your code......here is an example
    
if (a==1)
do something;
if (a==2)
do something;
if (a==3)
do something
.
.
.
.
.
.
.
if (a==25)
do something;

your program runs through 25 if statments regardless of whether it was the first in the list or the last
now if you had replaced those with else if statement.....then if a was equal to say 5....your program will skip the next 20 if statments......
not much but if you hava a lot of if statements it can add up

"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

Share this post


Link to post
Share on other sites
Else if is not an actual keyword. It is just the else and if keyword togther. The reason it is treated like a seperate keyword is because it follows those coding style rules for indenting (Please don''t be one of those people who double indents else if''s).

By the way blocks are now called compound statements, although it depends a little on the compiler.

Share this post


Link to post
Share on other sites
This thread can probably be deleted because it just reasserted what I already thought.

an else if is an else { if } is an else ... if

Share this post


Link to post
Share on other sites
They are NOT exactly the same. They are ALMOST the same, and in many situations can be used interchangebly. As for keywords of course there is no "else if" keyword, because it''s TWO words, both of which are already keywords, so it''s a non issue.

As for why it exists, there are two obvious reasons, to elimate the outrageous nesting that would be necesary using nested else{if} blocks, and to allow simple compiler optimization.

both arsenious and ncsu have valid statements, but I don''t think they realized that the original poster was not implying using multiple seperate if''s, but instead nested if''s in else blocks.

I do not believe there is anything that can be expressed in else if () syntax that cannot be eqivilently expressed in else{ if()} syntax, BUT it would be extremely unreadable, and would not at all match up visually with any of the flowchart based flow of control idioms that were common at the time of the creation of C, i mean really, when you draw a decision box with three branches, you want a coding structure with three branches, as simple as that, NOT a way to express that decision tree that in no way reflects the original idea ... see what I mean

A lot of a languages strength has to do with directness of translation, and I believe that is why the else if exists in the grammer ... or to be more acurate, because every other language in common usage has an equivelent, and every language I''ve seen made since does as well

Share this post


Link to post
Share on other sites
Of course with every { } there comes a new scope.
If that is what the programmer wishes for, then
let it be. However, as stated above, with ''if else''
and ''switch'', once the case is found, all other tests
are ignored.

if you do an


if (...)
{
}
else
{
int a;
(scope)
if (..)
{
int a;
(scope)
}
}


You created two levels of scope. While I had all the
compiler classes back in college (only one year later, and
I am already refering to it like it was a long time ago),
I am not sure the speed difference.


The main point I agree with McConnel (sp).
''Say it in Code''. Why confuse the reader with an embedded
''if'' withen an ''else'' if your purpose could be done with an
''if else''?

Share this post


Link to post
Share on other sites
If I am testing a single variable for more that 2 or 3 conditions i generally find a switch statement is more readable than a large number of if...elseif blocks...

Share this post


Link to post
Share on other sites
arsenius:
Actually you are wrong, you can still use else when you use the {} brackets, allow me to illustrate.
    if( conditionA )
{
statementA;
} else {
if( conditionB )
{
statementB;
} else {
if( conditionC )
{
statementC;
}
}
}[/source]
See? It is the same as having:
[source]if( conditionA )
statementA;
else
if( conditionB )
statementB;
else
if( conditionC )
statementC;


OneEyeLessThanNone

I roll dice for fun, I roll dice to live.

Share this post


Link to post
Share on other sites
Correct me if I''m wrong, but with a new scope also comes more overhead? I think the compiler have to save the state of some registers, and this should slow down the code.

Gaiomard Dragon
-===(UDIC)===-

Share this post


Link to post
Share on other sites
You''d think so, but most compilers barely listen to compound statements. It just allocates one just stack frame in the beginning.

For a good time hit Alt-F4! Go ahead try it, all the cool people are doing it.

Share this post


Link to post
Share on other sites
Hey guys! If you want 25 ifs and elseifs, you better come with a Select Case statement. Much less processor work (even on 3 statements) and much more readable...

Programming is:
A.The art of debugging a blank sheet of paper (or an empty file).
B.A pastime similar to banging one's head against a wall, but with fewer opportunities for reward.
C.The most fun you can have with your clothes on (although clothes are not mandatory).

Share this post


Link to post
Share on other sites
OneEyeLessThanNone:
what I said was right, because I said that you couldn''t have another else without nesting, which is what you did. And btw, that was really freaking hard to understand that code, until I formatted it how it would normally be done (or at least how I would type the same thing=)

later

arsenius


after three days without programming, life becomes meaningless

Share this post


Link to post
Share on other sites
Just in case anyone cares, I compiled a small program with VC++, and generated a listing for it. I compiled it once with a switch, and once with else if''s, using 4 cases. I just took a quick look, but the one with the switch did indeed seem to be shorter than the one with the else if''s, so it would seem that the switch would be faster...

With optomizations on, though, VC++ might generate the same code (just a thought).

Share this post


Link to post
Share on other sites
consider this piece of code

[source]
if(z == 1)
{
dosomething
}
if(z == 2)
{
dosomething
}
if(z == 3)
{
dosomething
}
else
{
dosomethingelse
}

the problem with that code is that if the first if = fals it defaults to the else
so if z = 2 it doesnt catch it.

but:

if u have
if(z == 1)
{
dosomething
}
else if(z == 2)
{
do something
}
else
{
something else}

it will check the variable agains all the ifs b4 defaulting to the else


+-----------------------------
Come, help me program my 3d game, i just picked up a new copy of Learn C++ in 21 days!
--Unknown Newbie
+-----------------------------

Share this post


Link to post
Share on other sites
You know... if you kids would just take a class, or read a book on introductory C++ programming, you wouldn''t be having this discussion.

Share this post


Link to post
Share on other sites
"else if" is not a keyword. You are using two keywords in sequence. "else" and "if".

You can have anything you want after an "else".

Share this post


Link to post
Share on other sites
There is no difference between the following two examples after compiling.


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


vs.


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


There are, arguably, cosmetic reasons for one or the other.

The first results in shorter lines and less bracket matching. Probably favored by lazy programmers who still choose ''i'' and ''j'' as variables.

I always use the second for consistency, because I feel it gives a better structure to the source (more whitespace) and it works for all cases of ''if-else'' structuring.

Try rewriting the following using ''else if''.


if()
{
}
else
{
i = BAD_VARIABLE_NAME;
j = BE_MORE_DESCRIPTIVE;
if(i == j)
{
}
else
{
i = LEARN_TO_TYPE;
j = LAZY_PROGRAMMERS;
if(i == j)
{
}
}
}


For all you beginners and non-professional coders, I, as a professional programmer, highly recommend consistency in style and coding methods. There is not one right way. Just don''t use 3 or 4 right ways because that makes it all wrong.

Mike Roberts
aka milo
mlbobs@telocity.com

Share this post


Link to post
Share on other sites
I agree with Milo''s reasoning about the consistency, but not all coders who use if-else ladders are lazy (and no, I''m not implicating myself).
Since Milo offers a challenge to re-write his example with an if-else ladder, it must either be impossible or damn difficult and code-ugly. But, in this case, who would have recognised it as an if-else possibility anyway?

-Mezz

Share this post


Link to post
Share on other sites