views:

559

answers:

10

input X:

if (0 <= X and X < 49)
    output "abc"
else if (50 <= X and X < 70)
    output "def"
else if (70 <= X and X < 85)
    output "ghi"
else if (85 <= X and X < 100)
    output "jkl"
endif
A: 

On edit: never mind, I'll agree I was wronfg on this one.

It's not, because no if is tested inside another if that tested true.

In particular, your example:

if (0 <= X and X < 49) output "abc"

else if (50 <= X and X < 70) output "def"

else if (70 <= X and X < 85) output "ghi"

else if (85 <= X and X < 100) output "jkl"

endif

could be rewritten as:

if (0 <= X and X < 49) output "abc"; return; end if

if (50 <= X and X < 70) output "def"; return; end if

if (70 <= X and X < 85) output "ghi"; return; end if

if (85 <= X and X < 100) output "jkl"; return; end if

// X < 0 or X >= 100

Comment:

An if statement doesn't have to be nested inside another if to be nested. It can be nested inside an else -Bill the Lizard

Point taken; I'll agree I'm wrong on this one.

tpdi
An if statement doesn't have to be nested inside another if to be nested. It can be nested inside an else.
Bill the Lizard
+8  A: 

You could think of it as being logically equivalent to the following:

if(a) {
    // code
} else {
    if(b) {
        // code
    } else {
        // code
    }
}

So in this respect, you could call it nested. In C and similar languages, this is exactly how it works since there's no "elseif" statement available. The curly braces are optional though, I just included them to make it clearer.

Turnor
You're exactly right; although this doesn't look nested, it probably is in most languages.
Jorn
A: 

It might be implemented as a nested loop, depending on language. However, the way you logically wrote it out, it wouldn't be considered one.

rlbond
+5  A: 

They are nested, but formatted like they are not.

Your code is the same as:

if (0 <= X and X < 49)
    output "abc"
else
    if (50 <= X and X < 70)
        output "def"
    else
        if (70 <= X and X < 85)
            output "ghi"
        else
            if (85 <= X and X < 100)
                output "jkl"
            endif
        endif
    endif
endif

This is not nested:

if (0 <= X and X < 49)
    output "abc"
endif
if (50 <= X and X < 70)
    output "def"
endif
if (70 <= X and X < 85)
    output "ghi"
endif
if (85 <= X and X < 100)
    output "jkl"
endif

This is valid in all (?) languages that have if statements (ignoring things like using {} instead of endif)

However, some languages have an actual "elseif" (or "elif") command, in which case you will not nest, but written as "else if" you can assume it's just a differently formatted nest.

GMan
It isn't the same as your first example. It has just one endif, not four!
Jonathan Leffler
+3  A: 

It depends on the actual language, and how it's written.

For example using VB, these If statements are not nested:

If 0 <= x And x < 49 Then
 output("abc")
ElseIf 50 <= x And x < 70 Then
 output("def")
ElseIf 70 <= x And x < 85 Then
 output("ghi")
ElseIf 85 <= x And x < 100 Then
 output("jkl")
End If

While these If statements are nested:

If 0 <= x And x < 49 Then
 output("abc")
Else
 If 50 <= x And x < 70 Then
  output("def")
 Else
  If 70 <= x And x < 85 Then
   output("ghi")
  Else
   If 85 <= x And x < 100 Then
    output("jkl")
   End If
  End If
 End If
End If
Guffa
I agree with your analysis.
Jonathan Leffler
+3  A: 

I would say yes they are nested. Your code is exactly equivalent to

if (0 <= X and X < 49)
    output "abc"
else
    if (50 <= X and X < 70)
        output "def"
    else
        if (70 <= X and X < 85)
            output "ghi"
        else
            if (85 <= X and X < 100)
                output "jkl"
endif

Notice that I've only changed the whitespace. When a language evaluates if...else if...else clauses, it tests each one until it finds the true clause (or it hits the final else). The nested ifs evaluate in exactly the same way. Also note that this isn't necessarily the case if there is an explicit elsif keyword.

One more thing I notice, the following is not equivalent to your code:

if (0 <= X and X < 49)
    output "abc"

if (50 <= X and X < 70)
    output "def"

if (70 <= X and X < 85)
    output "ghi"

if (85 <= X and X < 100)
    output "jkl"

The nesting is necessary to keep all of the text from being output when all of the conditions are true.

Bill the Lizard
Good observation that the conditions written are not mutually exclusive, so a rewrite as separate conditions requires fiendish care to ensure that there are not multiple outputs for a single input.
Jonathan Leffler
A: 

Of course they are nested. The second if clause is never reached, if the first one evaluates to FALSE. So, nested!

tharkun
+1  A: 

Given the syntax shown, I think the answer should be "No", contrary to the accumulated wisdom of the other answers.

You show:

if (0 <= X and X < 49)
    output "abc"
else if (50 <= X and X < 70)
    output "def"
else if (70 <= X and X < 85)
    output "ghi"
else if (85 <= X and X < 100)
    output "jkl"
endif

This is clearly a single if ... endif statement, and therefore there is no nesting. If there were multiple endif statements, it would be nested:

if (0 <= X and X < 49)
    output "abc"
else
    if (50 <= X and X < 70)
        output "def"
    else
        if (70 <= X and X < 85)
            output "ghi"
        else
            if (85 <= X and X < 100)
                output "jkl"
            endif
        endif
    endif
endif

So, in your language, it appears that the keyword elif (used by the Bourne shell) and elsif (used by Perl) and ElseIf (used by Visual Basic) is spelled else if.

If there was no explicit endif to mark the end of the statement, then I would be in agreement with the other answers - that the if statements (plural!) are nested, though the layout is perfectly sensible and recommended.

Jonathan Leffler
That single endif did bother me, but I allowed that it was pseudocode.
Bill the Lizard
A: 

No, it is not.

A nested statement is one that appears within a same statement, like If ... If. If ... ElseIf is all in the same "statement".

hmcclungiii
+1  A: 

This a somewhat pointless game of semantics; it depends on the syntax of the language involved.

For example in a C-like syntax they usually would be considered nested in the else clauses, with the braces omitted to obscure that fact. In this case Turnor's example is right.

In some other languages, like Python and VB, ‘else-if’ is an atomic construct of its own. In that case the ‘if’ can't be considered to be inside the ‘else’, so it couldn't be called “nested”.

if (0 <= X and X < 49)
    output "abc"
else if (50 <= X and X < 70)
    output "def"
endif

You haven't defined the syntax of your pseudocode sufficiently to say for sure, but the trailing ‘endif’ is suspicious. Its existence doesn't fit with the C-braces-omitted style; and the fact that there's only one of them and not more —

else
    if (50 <= X and X < 70)
        output "def"
    endif
endif

— means it doesn't match the with-braces (or begin/end) model either. So judging by that syntax I would put your pseudocode language in the ‘atomic else-if’ camp, and say quite arbitrarily: No, your if-statement is not nested.

(But you could always have defined a language where endifs are optional or whitespace-dependent. Or, you might have defined a language where the above program prints “Hello world” then deletes all your files. And has a built-in mail reader.)

bobince