Friday, September 3, 2010

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

Programmer Question

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



Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails