views:

609

answers:

11

Here we have a long-standing assumption that needs to be cleared up in my head. Is the following an example of nesting 'if' statements:

if (...)
  ...;
else if (...)
  ...;

I was under the impression that nesting required an 'if' inside another 'if', like so:

if (...)
  if (...)
    ...;

or at least a clear separation of scope when you nest inside an else, like so:

if (...)
  ...;
else { //if the next statement didn't 
       //exist, then the curly brace changes nothing?
  ...;
  if (...)
    ...;
}

This might boil down to how the compiler interprets things, whether the 'if' in else-ifs are considered in the same level as the parent if, or whether they create "new" 'if' statements. Thank you for your time!

edit: I ask because I am a TA in a java lab, and the topic of the day was nested-ifs. In the end, I found out that the teacher considered my first example to be valid for "nested if statements".

+7  A: 

A nested if is like this:

if (...)
  if (...)
    ...;

The first example you gave is just if/else.

A good way to remember it is that the indentation increases with a nested if, but with if/else it doesn't.

dcp
+1 Agree with this. Other one is an else if, this is a nested if.
bwawok
Isn't the second if in the first block the statement of the else block?Would if (...) ...; else \n if (...) ...;Be considered a nested if by your answer?
Jacob Tomaw
@Jacob Tomaw - Good question. In the raw sense, nested means inside, as Felix King pointed out in the comments to the OP's question. I would consider the code inside the else in his third example not to be a nested if from the first if statement, conversely, I would just consider it an if/else, and the else has more logic inside. I wouldn't consider the if inside the else part to be a nested if from the first if statement though.
dcp
This is not correct. The second if is actually nested within the else of the first--that's nesting (use braces around the if and else clauses to see it for yourself).
Bill K
@Bill K - So are you downvoting (assuming the downvote was from you) my comment or my answer? I never commented on the third example in my answer. Anyway, for what it's worth after reading your comments I tend to agree with your argument.
dcp
@dcp either you rewrote your answer or I really shouldn't have downvoted it. Sorry. I thought you actually said that if/else was a separate structure and was a little bothered about how highly it was voted when it was incorrect. If it wasn't so high I wouldn't have done that. I was trying to move the "Correct" answers up.
Bill K
+1  A: 

I understand it the same way you do.

If the implementation secretly only supports the "if" and "else" keywords, I suppose you could consider the "if" in "else if" to be an if statement nested inside an "else" statement. But I don't see this mattering all that much. The "else"'s scope contains only the "if" in this case, so it effectively behaves as a single scope.

Is this mild curiosity, or are you running into scoping issues?

bearcdp
I am a TA in a java lab, and the topic of the day was nested-ifs. In the end, I found out that the teacher considered my first example to be valid for "nested if statements".
swajak
That is weird, sounds like a conceptual ambiguity. Now I'll have to go ask my prof as well. This will eat away at my brain! :P
bearcdp
"If the implementation secretly only supports the "if" and "else" keywords" -- it's no secret, that's exactly what it is in most major languages (notable exception is perl). It's purely a stylistic choice to always write `else if` rather than nesting the `if` within the `else`.
rmeador
A: 

You can nest an if without declaring an else, which seems to be your confusion.

Joel
my confusion is whether "else-if" nests. I know if (...) if (...) is valid...
swajak
There's no such keyword as else-if in Java.
JeremyP
Ok, thanks. I didn't say it was a keyword...
swajak
+2  A: 

I would assume all three nested ifs. As there is a general agreement on the other cases, I concentrate on your first case:

This code is a nested if from my point of view, as a single if is something like

if(...) { } else { }

where the else part is optional. And you have effectively nested an if into the else part of the outer if else, even if the formatting (and maybe semantics) suggests it differently.

But in fact it boils down to the exact definition of "nested if", and I do not think there is a generally accepted one.

Why do you ask? If this is something with regards to code efficiency, then that is not purely dependent on nesting level. If this is due to some guidelines with regard to if nesting levels, then the guideline should make clear how exactly it defines "nested if".

Frank
I am a TA in a java lab, and the topic of the day was nested-ifs. In the end, I found out that the teacher considered my first example to be valid for "nested if statements".
swajak
A: 
Matt Groff
+13  A: 

Of course it's nested. Nesting has nothing to do with the way you format the code.

if (...)
{
    // some code
}
else if (...)
{
    // some code
}
else
{
    // some code
}

is exactly equivalent to

if (...)
{
    // some code
}
else
{
    if (...)
    {
        // some code
    }
    else
    {
        // some code
    }
}

There's no 'else if' keyword in Java, the effect is achieved purely by a formatting convention which stops long chains of nested elses from drifting right across the screen.

In case anybody needs persuading, here is the specification for an if statement: Java Language Specification section 14.9

JeremyP
thanks for the extra"of course" there, as if there is clear consensus on this (other commentators are disagreeing with you)
swajak
@swajak The other commentators are wrong, this is correct. HOWEVER, since "Nesting" is really only a problem conceptually, laying it out this way doesn't have the same conceptual burden as the other way so in effect it is not increasing the mental complexity the way a nested if would.
Bill K
I was writing up a similar answer, but @JeremyP captures my thoughts precisely [even the "of course :-)]. In terms of "how the compiler interprets things", `else if` is not part of the Java language syntax like it is in some other languages. As such, I would say that a nested-`if` is any `if` statement within the `then`-block or `else`-block of another `if` statement. Usage of `else if` in Java code is really a nested-`if` embedded as the only statement of an else code block.
Bert F
Somehow, I never thought of `else if` that way. But now that you say it, it makes a lot of sense.
Chinmay Kanchi
This is what we're taught at school.
arscariosus
@swajak: the commetators that disagree with me should take a look at the Java language specification. http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.9
JeremyP
Why the down vote?
JeremyP
+2  A: 

I don't consider the first form (if/else if/etcetera) to be "nested". Rather, its more akin to a switch block, which I also would not consider to be nested.

George Jempty
+8  A: 

This is an if statement:

if (condition1) {
    // ...
}

This is a nested if statement:

if (condition1) {
    // ...
    if (condition2) {
        // ...
    }
    // ...
}

This is an if-else statement:

if (condition1) {
    // ...
} else {
    // ...
}

This is a chained if-else statement:

if (condition1) {
    // ...
} else if (condition2) {
    // ...
}
BalusC
And my third example is?
swajak
Depends on whether the `else` contains any logic. The outer part is an `if-else` statement, the inner part is a nested `if` statement. If the `else` doesn't contain any logic (so you can omit the braces of the `else`), then it becomes a chained `if-else` statement.
BalusC
A: 

If it is placed inside another if block it is a nested if. The first case is an if-else statement.

fastcodejava
+4  A: 

Conceptually, for me the first form

if (cond1)
{
}
else if (cond2)
{
}
else
{
}

is not nested because all conditions are evaluated at the same logical level. In other words, I read this as "branch your action based on cond1:cond2:anything else".

if (cond1){ 
   if (cond2){ 
     if (cond3){ 

Is nested because you have conditions evaluated within other conditions , that is, dependent on previous conditions. If you want to be literal about it, the scope of cond2 in the second case is literally nested in the scope of cond1. This is not true in the first form.

I never thought this was controversial.

edit: if you've ever had to read code like this the difference might seem less academic.

Steve B.
A: 

In the construction

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

The else identifies an implicit if: if the antecedent is false.

Having an if nested within that else scope, then, in my opinion, semantically qualifies as a nested if statement.

Noel Ang