tags:

views:

405

answers:

8

I happen to read the practicing material for SCJP certification, and I just tripped over a chapter of flow control et al, where they give the impression that "else if" is a keyword on its own. I have always thought that it was just a normal else, containing nothing but an if block, braces omitted.

So, which is it?

Edit: I'd like to emphasize that this question is more in the "pique my interest" category than the "serious business" one.

+6  A: 

it would be two separate keywords indicated by the space between the two words.

zPesk
+5  A: 

It actually says

else {
    if { ... }
}
Zack
} else if(true/false){}Also works too.
scheibk
A: 

I'm no Java expert, but in C it's certainly defined as two different keywords, and Java stole pretty liberally from that part of C's syntax.

Regardless, outside of silly questions on exams, it won't ever make a difference in practice. The effect is identical either way.

C Pirate
Yeah, I'm aware of the practical meaninglessness. For some strange reason, I want to know these kinds of irrelevant pieces of data.
Henrik Paul
+8  A: 

Yes, they are two separate keywords—the Java language specification does not specify an else if keyword. It is actually, as the other posters here have said, an if statement contained inside of an else statement.

EDIT: A lot of training/educational materials seem to imply that else if is actually a keyword (and, indeed, it is usually treated as such), but I am inclined to think that this is more for the sake of clarity/simplicity, with the consequence of sacrificing accuracy.

htw
+1  A: 

If you open your assembly with a debugger you'd recognise its an "if" statement.

John
+1  A: 

Two separate keywords I believe. But i'm positive you cannot have an else without an if.

Java Lexical Structure

edwardTheGreat
Are you seriously saying that you cannot have an else statement? :P
Daniel Lew
ha you know what I mean...
edwardTheGreat
+3  A: 

It's two keywords.

else may be followed by a block or a single statement.

in the case of else if. The statement is another if statement.

Update

It wouldn't hurt to mention that else if is a common style choice for deeply nested if statements and makes this situation much more readable than nesting. Unless I need the fall-through behaviour possible with the switch statement, I use else if instead.

Arnold Spence
A: 

While some languages do have 'elsif', 'elseif', or 'else if' as a separate statement (somewhat like a 'case' statement), Java is not one of them.