简体   繁体   English

c++ 标准中的 if..else 语句

[英]if..else statement in the c++ standard

From the C++ standard section 6.4.1: The if statement :来自 C++ 标准第 6.4.1 节: if 语句

If the condition (6.4) yields true the first substatement is executed.如果条件 (6.4) 为真,则执行第一个子语句。 If the else part of the selection statement is present and the condition yields false, the second substatement is executed.如果存在选择语句的 else 部分并且条件产生假,则执行第二个子语句。 In the second form of if statement (the one including else), if the first substatement is also an if statement then that inner if statement shall contain an else part.在 if 语句的第二种形式(包括 else 的那个)中,如果第一个子语句也是一个 if 语句,则该内部 if 语句应包含 else 部分。

Section 6.4: Selection statements :第 6.4 节:选择声明

Selection statements choose one of several flows of control.
    selection-statement:
        if ( condition ) statement
        if ( condition ) statement else statement
    condition:
       expression
       attribute-specifier-seqopt decl-specifier-seq declarator = initializer-clause
       attribute-specifier-seqopt decl-specifier-seq declarator braced-init-list

I thought that else if() {} statement was a separate statement from if() {} and else {} .我认为else if() {}语句是与if() {}else {}分开的语句。 Now it seems that this else if {} statement is just an else statement with it's own if() {} inside it so these two codes are equal:现在看来这个else if {}语句只是一个 else 语句,里面有它自己的if() {}所以这两个代码是相等的:

if(condition) {

    }
    else {
        if(condition) {

        }
    }

if(condition) {

    }
    else if(condition) {

    }

Now what if we have multiple else if-s?现在如果我们有多个 else if-s 呢? These codes are also equal in C++:这些代码在 C++ 中也相同:

if(condition) {

    }
    else {
        if(condition) {

        }
        else {
            if(condition){

            }
        }
    }

if(condition) {

    }
    else if {

    }
    else if {

    }

About the last code: When we write an else statement without curly braces only the first statement is associated to the else because the other statements are not part of that else (they are not in curly braces with the first statement).关于最后一个代码:当我们编写一个没有大括号的 else 语句时,只有第一条语句与else相关联,因为其他语句不是else 的一部分(它们不在第一条语句的大括号中)。 So isn't it logical for the compiler to say that the second else is not associated with an if statement?那么编译器说第二个else与 if 语句无关,这不是合乎逻辑的吗?

if (condition) statement else statement

is a single selection-statement .是一个单一的选择语句 This means that the entire if...else is the substatement of a previous else .这意味着整个if...else是前一个else的子语句。

or in other words, you start rolling up the statements from the bottom.或者换句话说,您开始从底部开始汇总报表。

The else is part of the if statement it corresponds to. else是它对应的if语句的一部分。 In the case of:如果是:

if(condition) {

}
else if {

}
else if {

}

The nested statements are as follows:嵌套语句如下:

if (condition) { } else // first statement
  if { } else           // second statement
    if { }              // third statement

So the second else is associated with the second if .所以第二个else与第二个if相关联。

The reason for these rules is to resolve a problem known as the Dangling else problem.这些规则的原因是为了解决一个称为Dangling else问题的问题。

It rears its head in code like this...它在这样的代码中抬起头......

if (A) 
if (B) DoB();
else DoC();

When is DoC() performed? DoC()什么时候执行? Does it happen when A is false ?Afalse时会发生吗? Or does it happen when A is true and B is false ?还是在AtrueBfalse

There is no logical way for the compiler to resolve this kind of situation.编译器没有逻辑方法来解决这种情况。 So outside the normal parsing rules, 6.3 declares that in this ambiguous case, the else belongs to the inner if statement.所以在正常的解析规则之外,6.3 声明在这种模棱两可的情况下,else 属于内部if 语句。

不,编译器说第二个 else 与 if 语句无关是不合逻辑的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM