输入 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
输入 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
你可以认为它在逻辑上等同于以下内容:
if(a) {
// code
} else {
if(b) {
// code
} else {
// code
}
}
所以在这方面,你可以称之为嵌套。在 C 和类似语言中,这正是它的工作方式,因为没有可用的“elseif”语句。花括号是可选的,但我只是将它们包括在内以使其更清晰。
它们是嵌套的,但格式与它们不是一样。
您的代码与以下内容相同:
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”你可以假设它只是一个不同格式的嵌套。
这取决于实际的语言以及它的编写方式。
例如使用 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
我会说是的,它们是嵌套的。您的代码完全等同于
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
子句时,它会测试每一个子句,直到找到真正的子句(或者它达到 final else
)。嵌套if
的 s 以完全相同的方式进行评估。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"
当所有条件都为真时,嵌套对于防止输出所有文本是必要的。
鉴于显示的语法,我认为答案应该是“否”,这与其他答案的累积智慧相反。
你展示:
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
语句(复数!)是嵌套的,尽管布局是完全明智和推荐的。
这是一种毫无意义的语义游戏;这取决于所涉及语言的语法。
例如,在类似 C 的语法中,它们通常被认为嵌套在 else 子句中,省略大括号以掩盖这一事实。在这种情况下,特纳的例子是正确的。
在其他一些语言中,如 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 大括号的样式;而且只有其中一个而不是更多的事实——</p>
else
if (50 <= X and X < 70)
output "def"
endif
endif
— 表示它也不匹配带大括号(或开始/结束)模型。因此,根据该语法判断,我会将您的伪代码语言放在“原子 else-if”阵营中,并且相当武断地说:不,您的 if 语句没有嵌套。
(但您总是可以定义一种语言,其中 endifs 是可选的或依赖于空格。或者,您可能已经定义了一种语言,上面的程序打印“Hello world”然后删除所有文件。并且有一个内置的邮件阅读器。 )
它可能被实现为嵌套循环,具体取决于语言。但是,按照您逻辑上写出来的方式,它不会被视为一个。
不它不是。
嵌套语句是出现在同一语句中的语句,例如 If ... If。If ... ElseIf 都在同一个“语句”中。
编辑时:没关系,我同意我在这个问题上是错误的。
不是,因为 noif
在另一个经过测试的内部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 语句不必嵌套在另一个 if 中。它可以嵌套在 else -Bill the Lizard 中
取点;我同意我在这一点上错了。