这个 IF 语句是否嵌套?
输入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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以将其视为逻辑上等同于以下内容:
因此,在这方面,您可以将其称为嵌套。 在 C 和类似语言中,这正是它的工作原理,因为没有可用的“elseif”语句。 不过,大括号是可选的,我只是将它们包括在内以使其更清晰。
You could think of it as being logically equivalent to the following:
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.
它们是嵌套的,但格式却与非嵌套的一样。
您的代码与以下内容相同:
This is notnested:
This is valid in all (?) languages that has if statements (忽略诸如使用 {} 而不是 endif 之类的事情)
但是,某些语言具有实际的“elseif”(或“elif”) ") 命令,在这种情况下,您将不会嵌套,但写为“else if”时,您可以假设它只是一个不同格式的嵌套。
They are nested, but formatted like they are not.
Your code is the same as:
This is not nested:
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.
这取决于实际的语言及其书写方式。
例如,使用 VB,这些
If
语句不是嵌套的:而这些
If
语句是嵌套的:It depends on the actual language, and how it's written.
For example using VB, these
If
statements are not nested:While these
If
statements are nested:我会说是的,它们是嵌套的。 您的代码完全等同于
注意我只更改了空格。 当一种语言计算
if...else if...else
子句时,它会测试每个子句,直到找到 true 子句(或者命中最后的else
)。 嵌套的if
的计算方式完全相同。 另请注意,如果存在显式elsif
关键字,则情况不一定如此。我注意到的另一件事是,以下内容与您的代码不等效:
嵌套是必要的,以在所有条件都为真时防止输出所有文本。
I would say yes they are nested. Your code is exactly equivalent to
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 finalelse
). The nestedif
s evaluate in exactly the same way. Also note that this isn't necessarily the case if there is an explicitelsif
keyword.One more thing I notice, the following is not equivalent to your code:
The nesting is necessary to keep all of the text from being output when all of the conditions are true.
鉴于所示的语法,我认为答案应该是“否”,这与其他答案所积累的智慧相反。
您显示:
这显然是一个
if
...endif
语句,因此没有嵌套。如果有多个
endif
语句,它将被嵌套:因此,在您的语言中,关键字
elif
(由 Bourne shell 使用)和elsif
(由 Perl 使用)和ElseIf
(由 Visual Basic 使用)拼写为else if
。如果没有明确的
endif
来标记语句的结束,那么我会同意其他答案 -if
语句(复数!)是嵌套的,尽管布局非常合理并且值得推荐。Given the syntax shown, I think the answer should be "No", contrary to the accumulated wisdom of the other answers.
You show:
This is clearly a single
if
...endif
statement, and therefore there is no nesting.If there were multiple
endif
statements, it would be nested:So, in your language, it appears that the keyword
elif
(used by the Bourne shell) andelsif
(used by Perl) andElseIf
(used by Visual Basic) is spelledelse 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 theif
statements (plural!) are nested, though the layout is perfectly sensible and recommended.这是一个毫无意义的语义游戏; 这取决于所涉及语言的语法。
例如,在类似 C 的语法中,它们通常被视为嵌套在 else 子句中,省略大括号以掩盖这一事实。 在这种情况下,特纳的例子是正确的。
在其他一些语言中,例如 Python 和 VB,“else-if”是其自己的原子结构。 在这种情况下,“if”不能被视为位于“else”内部,因此不能称为“嵌套”。
您还没有充分定义伪代码的语法来确定,但尾随的“endif”是可疑的。 它的存在并不符合省略C大括号的风格; 事实上,只有其中一个而不是更多 —
— 意味着它也不匹配带大括号(或开始/结束)模型。 因此,根据该语法来判断,我会将您的伪代码语言置于“原子 else-if”阵营,并相当任意地说:不,您的 if 语句不是嵌套的。
(但您始终可以定义或者,您可能定义了一种语言,其中上述程序打印“Hello world”,然后删除所有文件。)
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”.
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 —
— 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.)
它可能会实现为嵌套循环,具体取决于语言。 然而,按照你逻辑上写出来的方式,它不会被认为是一个。
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.
不它不是。
嵌套语句是出现在同一语句中的语句,例如 If ... If。 If ... ElseIf 都在同一个“语句”中。
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".
编辑时:没关系,我同意我在这一点上是错误的。
事实并非如此,因为在另一个测试
true
的if
中没有测试任何if
。特别是,您的示例:
可以重写为:
评论:
Point 内; 我承认我在这一点上是错的。
On edit: never mind, I'll agree I was wronfg on this one.
It's not, because no
if
is tested inside anotherif
that testedtrue
.In particular, your example:
could be rewritten as:
Comment:
Point taken; I'll agree I'm wrong on this one.