Jump to content
  • Advertisement

Archived

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

iwasbiggs

point of 'else if' ?

This topic is 6571 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!