这个IF语句是否嵌套?

时间:2009-04-10 23:47:19

标签: nested pseudocode

输入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

9 个答案:

答案 0 :(得分:8)

你可以认为它在逻辑上等同于以下内容:

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

所以在这方面,你可以称之为嵌套。在C和类似的语言中,这正是它的工作原理,因为没有“elseif”语句可用。花括号是可选的,我只是将它们包括在内以使它更清晰。

答案 1 :(得分:5)

它们是嵌套的,但格式不同。

您的代码与:

相同
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

这不是嵌套的:

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

这在所有(?)语言中都有效,这些语句包含if语句(忽略使用{}而不是endif的内容)

但是,某些语言有一个实际的“elseif”(或“elif”)命令,在这种情况下你会嵌套,但写成“else if”你可以假设它只是一个不同的格式化的巢。

答案 2 :(得分:3)

这取决于实际语言及其编写方式。

例如,使用VB,这些If语句不是嵌套的:

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

这些If语句是嵌套的:

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

答案 3 :(得分:3)

我会说是的,它们是嵌套的。您的代码完全等同于

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

请注意,我只更改了空格。当一种语言评估if...else if...else子句时,它会测试每一个子句,直到找到真正的子句(或者它到达最终的else)。嵌套的if以完全相同的方式进行评估。另请注意,如果存在明确的elsif关键字,则情况并非如此。

我注意到的另一件事是,以下内容与您的代码不等同

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"

嵌套是必要的,以便在所有条件都为真时保持输出所有文本。

答案 4 :(得分:1)

鉴于所显示的语法,我认为答案应为“否”,这与其他答案的积累智慧相反。

你表示:

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

这显然是单个if ... endif语句,因此没有嵌套。 如果有多个endif语句,则会嵌套:

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

因此,在您的语言中,似乎关键字elif(由Bourne shell使用)和elsif(由Perl使用)和ElseIf(由Visual Basic使用)是拼写为else if

如果没有明确的endif来标记语句的结尾,那么我会同意其他答案 - if语句(复数!)是嵌套的,尽管布局非常明智和推荐。

答案 5 :(得分:1)

这是一种有点无意义的语义游戏;这取决于所涉及语言的语法。

例如,在类似C语法的语法中,它们通常被认为嵌套在else子句中,省略了括号来掩盖这一事实。在这种情况下,Turnor的例子是正确的。

在其他一些语言中,如Python和VB,'else-if'是它自己的原子构造。在这种情况下,'if'不能被认为是在'else'中,所以它不能被称为“嵌套”。

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

您还没有充分确定您的伪代码的语法,但是尾随的'endif'是可疑的。它的存在不符合C-braces省略的风格;事实上,只有一个而不是更多 -

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

- 表示它与with-braces(或开始/结束)模型不匹配。因此,根据该语法判断,我会将您的伪代码语言放在'atomic else-if'阵营中,并且相当随意地说:不,你的if语句不是嵌套的。

(但你总是可以定义一种语言,其中endifs是可选的或依赖于空白的。或者,你可能已经定义了一种语言,上面的程序打印“Hello world”然后删除你的所有文件。并且内置​​了邮件阅读器。)

答案 6 :(得分:0)

它可能被实现为嵌套循环,具体取决于语言。但是,从逻辑上写出它的方式,它不会被认为是一个。

答案 7 :(得分:0)

不,不是。

嵌套语句是出现在同一语句中的语句,如If ... If。如果...... ElseIf都在同一个“声明”中。

答案 8 :(得分:-1)

编辑:没关系,我会同意我对这个问题感到满意。

不是,因为在测试if的另一个if内没有测试true

特别是你的例子:

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

可以改写为:

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

注释:

  

如果要嵌套,if语句不必嵌套在另一个语句中。它可以嵌套在else -Bill the Lizard

采取的点;我会同意我错了。

相关问题