开发者

In Java, is this considered an example of a "nested IF statement"?

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 n开发者_如何学JAVAothing?
  ...;
  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".


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


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) {
    // ...
}


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.


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.


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".


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.


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?


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


The statement actually is an example of nesting in an irregular form.

The form you have is:

if(statement1 == true)
   code1...;
else if (statement2 == true)
   code2...;

This is equivalent to the following:

if (statement1 == false)
   if (statement2 == true)
      code2...;
else
   code1;

However, I think it's a poor programming practice to nest in this form. You should be able to rearrange code to fit a more traditional form.


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


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.


nested if's statement mean: in the if condition placed another if condition

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜