C# 中两个 if 和一个 else。会发生什么?

发布于 2024-11-18 06:55:37 字数 348 浏览 2 评论 0原文

例如:

if(cond1)
if(cond2) DoCond2();
else DoCond3();

else 语句会被视为第一个 if 或第二个 if 的 else 吗?为什么? (有从语法角度解释吗?) 其他基于 C 的编程语言(例如 C 和 Java)的答案是否也相同?

注意:这不是家庭作业。我可以轻松测试它,如果我不喜欢默认行为,则可以使用大括号,但我很好奇原因。

编辑 伙计们,显然原来的例子中有一个非常严重的错误。请再次检查。我知道,这很尴尬。对不起。

for example:

if(cond1)
if(cond2) DoCond2();
else DoCond3();

Will the else statement be considered as the else of the first if or the second if? Why? (is there an explanation from syntactic point of view?)
Is the answer also the same in the other C-based programming languages such as C and Java?

Note: this is not homework. I can easily test it, and use the curly brackets if I don't like the default behaviour, but I'm curious as to the reason.

Edit Guys, apparently there was a very serious mistake in the original example. Please check again. I know, it's embarrassing. Sorry.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

风月客 2024-11-25 06:55:37

根据 MSDN

在嵌套 if 语句中, else 子句属于最后一个如果没有相应的else。

同样,C 和 C++ 也可以计算条件,其中 0 的结果为假,任何其他数字为真。在 C# 中,条件必须计算为布尔值 true/false

编辑
根据编辑,它是一个嵌套的 if,因此内部 if 为 true 时进行评估,而 else 仍然与内部 if 一起使用

As per MSDN

In nested if statements, the else clause belongs to the last if that does not have a corresponding else.

Also C and C++, conditions can be evaluated where a result of 0 is false and any other number is true. In C#, the condition must evaluate to a boolean value true/false

EDIT
As per the Edit its a nested if, so the inner one is evaluated when true and still the else is with the inner if

孤城病女 2024-11-25 06:55:37

else 语句会被视为第一个 if 或第二个 if 的 else 吗?

编辑(删除了我原来在这里所说的内容 - 它不适用):

有了这样的结构,我无法告诉你。每种语言的语法都是不明确的。

但我个人猜测它会“坚持”到最接近的if。否则,人们几乎不可能阅读。

我想说尝试一下,但最好的解决方案是应用禁止人们这样做的编码标准......

为什么? (有没有从句法角度解释一下?)

您可以查看每种语言的(通常是非官方的)语法定义,以了解为什么这些语句会以它们的方式组合在一起。

(旧)官方 C# 语法:

http:// msdn.microsoft.com/en-us/library/aa645596(v=VS.71).aspx

编辑: 但这对你没有多大帮助,因为语法对于这种情况下的类 C 语言,需要上下文或故意的解决方法。

请参阅:http://en.wikipedia.org/wiki/Dangling_else#Context_Free_Solutions

其他基于 C 的编程语言(例如 C 和 Java)的答案是否也相同?

语法是复杂的事情,正确设置嵌套范围有点困难。但总的来说,答案通常围绕块与语句的定义。

您可以将语句定义为以;结尾的行。然后将定义为由{ }包围的一系列语句

因此,您可以将 if 语句定义为 if block-or-statement [else block-or-statement],即 [ ] 表示它是可选的。

编辑:这个定义的问题是这样的语法需要上下文,并且只能用问题的原始表述来解决问题。当它确实不明确时(作为您的新编辑版本),您必须进行更高级别的处理(上下文)来处理歧义。

C++,例如:(

在最后一个文档中,您会多次看到“

这个问题存在于许多语言中,而不仅仅是 C 语言。这是您在 BASIC 变体中看到诸如 end if 之类的显式语句的原因之一,或 Pythonif 规则。

Will the else statement be considered as the else of the first if or the second if?

Edit (removed what I original said here - it doesn't apply):

With such a construction, I couldn't tell you. The syntax for each language is ambiguous.

But I'd personally guess it would "stick" to the closest if. Otherwise it would be nearly impossible for someone to read.

I'd say try it and see, but the best solution is to apply a coding standard that forbids people from ever doing this...

Why? (is there an explanation from syntactic point of view?)

You can check out the (often unofficial) grammar definition for each language to see why such statements hang together the way they do.

The (old) official C# grammar:

http://msdn.microsoft.com/en-us/library/aa645596(v=VS.71).aspx

Edit: But it won't help you much, because the grammar for C-like languages in this case requires context, or deliberate work-arounds.

See: http://en.wikipedia.org/wiki/Dangling_else#Context_Free_Solutions

Is the answer also the same in the other C-based programming languages such as C and Java?

Grammars are complicated things, and getting nested scopes right is a bit hard. But in general, the answer will often revolve around definitions of blocks vs statements.

You could define a statement as a line ended by a ;. Then define a block as a series of statements surrounded by { }.

So, you could define your if statement as if block-or-statement [else block-or-statement], the [ ] denoting that it is optional.

Edit: The problem with this definition is that such a grammar requires context, and only solves the problem with the original formulation of your question. When it is truly ambiguous (as your new, edited version), you must have higher level processing (context) to deal with the ambiguity.

C++, for example:

A few times in that last document you will see "dangling else". This specifically describes what you're looking for.

This problem exists in many languages, not just C-likes. It is one reason you see explicit statements like end if in BASIC variants, or Python's rules for ifs.

木緿 2024-11-25 06:55:37

让我们澄清一下 if 语句的工作原理。

if 子句之后,您可以有两件事:

  • 单行语句
  • 多行语句

单行语句就像您提供的两个示例。在这种情况下,仅当 else 直接位于 if 之后时才属于 if (就像您的第二个示例)。

多行语句将在 if 语句后添加大括号 { }。右括号后面的 else 使其属于该语句。

您可以使用 else if 语法“链接”if 语句。下面的 if 的行为与普通 if 类似,但仅当前面的 if 返回 false 时才会进行检查。在您的示例中,无论结果如何,都会检查两个 if 语句。

Let's clarify how the if statement works.

After an if clause, you can have two things:

  • Single line statement
  • Multi line statement

The single line statement is like the two examples you provided. In that scenario, the else belongs to the if only if it is directly after it (like your second example).

The multi line statement will have curly brackets { } following the if statement. An else following the closing bracket makes it belong to that statement.

You can "chain" if statements using the else if syntax. The following if will behave like a normal if, but it will be checked only if the previous if returns false. In your example, both if statements will be checked regardeless of the result.

寻找一个思念的角度 2024-11-25 06:55:37

也许要理解这一点,添加大括号会很有用:

if(cond1) {
    DoCond1();
}
if(cond2) {
    DoCond2();
} else {
    DoCond3();
}

如果 ifelse 之后没有大括号,则只有第一个语句(在本例中为 DoCondX code>) 属于 if/else 块。

编辑查看编辑后的代码:

if(cond1) {
    if(cond2) {
        DoCond2();
    } else {
        DoCond3();
    }
}

Maybe to understand this it would be useful to add braces:

if(cond1) {
    DoCond1();
}
if(cond2) {
    DoCond2();
} else {
    DoCond3();
}

If there are no braces after if or else, only the first statement (in this case DoCondX) belongs to the if/else block.

EDIT Looking at the code after the edit:

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