while 和 for 循环的嵌套默认值

Nesting default of while and for loop

我很好奇在没有括号的情况下,for 循环和 while 循环主体中的默认嵌套是什么。

I.E.

while (condition) //without brackets
if (condition)
else

for (condition) 
if (condition)
else

对比

while (condition)
for (condition)
if (condition
else

我知道如果没有括号出现,for 循环将嵌套一个 else,如果它的主体中有一个 if。 while 循环会发生什么?会一样吗?而且,在条件中包括 while 和 for 循环?会不会变成像

while (condition) {
   for (condition) {
       if (condition)
       else
   } // for
} //while

如果 for 循环中有另一组 if 和 else 会怎样?它会被忽略成

for (condition)
    if (condition)
    else 
if (condition)
else

还有其他我应该注意的情况吗?

在你所有的例子中,没有一个甚至是微妙的混淆点。只有一种方法可以解释其中的任何一个,以便它可以编译。自己尝试一下:在每个代码片段中放置大括号('{' 和 '}')并尝试赋予它 2 种不同的含义。你将无法做到这一点。

重点是所有循环都可能迭代一个控制块,它可能是:

  1. 单个控制块(例如语句),或
  2. 多个控制块按{...}分组为一个。

在你的情况下,

while (condition)
for (condition)
if (condition)
else

if...else形成一个单独的控制块,for也是如此;因此,这与

明确相同
while (condition) {
    for (condition) {
        if (condition) {
           ...
        } else {
           ...
        }
    }
}

循环和条件表达式没有特定的嵌套规则。你只需要记住,如果你不使用大括号,循环只会执行一条语句。

通常总是使用大括号是个好主意。

while (condition) 适用于下一个 语句 。我们经常使用复合语句,即用大括号括起来的一行或多行代码:

while (condition)
{
/* code goes here */
}

但该语句不必是复合语句:

while (condition)
    std::cout << "I'm still in the condition.\n";

有时语句会更复杂:

while (condition)
    if (some_other_condition)
        /* some code here */
    else
       /* some other code here */

并且该语句可以是循环语句:

while (condition)
    while (another_condition)
        while (yet_another_condition)
            /* whew, we got here! */

两个迭代语句的定义如下

while ( condition ) statement
                    ^^^^^^^^^
for ( for-init-statement conditionopt; expressionopt) statement
                                                      ^^^^^^^^^ 

其中 语句 是包含复合语句或空语句的任何语句。

例如在这个结构中

while (condition) //without brackets
if (condition) statement
else statement

语句是if-else语句

if (condition) statement
else statement

您可以将其括在大括号中,形成包含单个 if-else 语句的复合语句

while (condition) //without brackets
{
if (condition) statement
else statement
}

但是效果是一样的

在此构造中

while (condition)
for (condition)
if (condition) statement
else statement

while 循环将 for 循环作为其语句,而 for 循环又将 if-else 作为其自身的语句。你可以这样写

while (condition)
{
    for (condition)
    {
        if (condition) statement
        else statement
    }
}

然而,这两个复合语句只包含一个语句。所以这两个结构是等价的。

没有歧义;让我们看看实际的语法(C++11 §A.5):

whileforiteration-statement

iteration-statement:
    while (condition) statement
    do statement while ( expression)
    for ( for-init-statement conditionopt; expressionopt) statement

(每行是一个替代模式,满足第一行冒号之前的术语;斜体字是对其他语法规则的引用;opt 标记可选术语)

ifif/elseselection-statement:

selection-statement:
    if ( condition ) statement
    if ( condition ) statement else statement
    switch ( condition ) statement

现在,如您所见,它们都需要一个,单一的,通用的语句”来执行,没有明确提及大括号; 语句,反过来定义为:

statement:
    labeled-statement
    attribute-specifier-seqopt expression-statement
    attribute-specifier-seqopt compound-statement
    attribute-specifier-seqopt selection-statement
    attribute-specifier-seqopt iteration-statement
    attribute-specifier-seqopt jump-statement
    declaration-statement     attribute-specifier-seqopt try-block

让我们忘记 属性说明符序列opt;带回家的要点是 statement 反过来可以是上面的 while/for/if 中的任何一个,加上一些其他东西,包括 复合语句 ,即大括号:

compound-statement:
    { statement-seqopt }
statement-seq:
    statement
    statement-seq statement

(这是一种正式的说法,可以在大括号内放置一个或多个 语句 s)

现在,有了这些知识,我们可以像解析器一样通过机械思考来解开您的示例:

for (condition)
if (condition)
doA();
else
doB();
if (condition)
doC();
else
doD();
  • for()的东西要一个语句;让我们尝试解析以下内容:

    • 有一个ifif 要么只需要 语句 语句 后跟 else 和另一个语句;让我们看看:

      • doA() 是一个 表达式语句
      • else 关注
      • doB() 是一个 表达式语句

      因此整个if (condition) doA(); else doB();是一个语句,特别是选择语句

    好的,我们有完整的语句作为循环体提供给for,因此for语句完成;

  • 然后,我们有一个if-else,如上解析

    • doC() 是一个 表达式语句
    • else 关注
    • doD() 是一个 表达式语句

因此,最终的"braced"解释为

for (condition) {
    if (condition) {
        doA();
    } else {
        doB();
    }
}
if (condition) {
    doC();
} else {
    doD();
}