if()
{
}
else
{
if()
{
}
}
Edited by - iwasbiggs on 6/16/00 11:58:59 PM
point of 'else if' ?
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.
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.
Also, in C/C++, isn''t there an actual elseif statement...like the following
if SomethingSomething
{
}
elseif somethingelse
{
}
------------------------
Captured Reality.
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...
if(x == 10)
{
}
else if(x == 5)
{
}
Although that''s just a different way of formatting the if statements...
ah yes.
I see now. It could also be written like this then
else{ if{
although they would be exactally the same huh?
------------------------
Captured Reality.
I see now. It could also be written like this then
else{ if{
although they would be exactally the same huh?
------------------------
Captured Reality.
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!
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!
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
so instead we use this
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
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
slight as it may be.....else if statements also improve speed of your code......here is an example
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
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
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.
By the way blocks are now called compound statements, although it depends a little on the compiler.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement