这个 IF 语句是否嵌套?

发布于 2024-07-17 21:45:22 字数 246 浏览 8 评论 0原文

输入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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

习惯成性 2024-07-24 21:45:22

您可以将其视为逻辑上等同于以下内容:

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

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

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.

遥远的绿洲 2024-07-24 21:45:22

它们是嵌套的,但格式却与非嵌套的一样。

您的代码与以下内容相同:

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 notnested:

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 has if statements (忽略诸如使用 {} 而不是 endif 之类的事情)

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

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.

强者自强 2024-07-24 21:45:22

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

例如,使用 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

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
北方的韩爷 2024-07-24 21:45:22

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

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 子句时,它会测试每个子句,直到找到 true 子句(或者命中最后的 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"

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

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.

梦里兽 2024-07-24 21:45:22

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

您显示:

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 语句(复数!)是嵌套的,尽管布局非常合理并且值得推荐。

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.

逆蝶 2024-07-24 21:45:22

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

例如,在类似 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大括号的风格; 事实上,只有其中一个而不是更多 —

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

— 意味着它也不匹配带大括号(或开始/结束)模型。 因此,根据该语法来判断,我会将您的伪代码语言置于“原子 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”.

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

愿与i 2024-07-24 21:45:22

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

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.

挥剑断情 2024-07-24 21:45:22

不它不是。

嵌套语句是出现在同一语句中的语句,例如 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".

旧人哭 2024-07-24 21:45:22

编辑时:没关系,我同意我在这一点上是错误的。

事实并非如此,因为在另一个测试 trueif 中没有测试任何 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 (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 中

Point 内; 我承认我在这一点上是错的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文