Hanging else problem?

后端 未结 4 1158
轮回少年
轮回少年 2021-01-14 03:12

What is the \"hanging else\" problem? (Is that the right name?)

Following a C++ coding standard (forgot which one) I always use brackets (block) with control structu

4条回答
  •  臣服心动
    2021-01-14 03:37

    Which if does the else belong to?

    if (a < b)
        if (c < d)
            a = b + d;
        else
            b = a + c;
    

    (Obviously you should ignore the indentation.)

    That's the "hanging else problem".

    C/C++ gets rid of the ambiguity by having a rule that says you can't have an-if-without-an-else as the if-body of an-if-with-an-else.

提交回复
热议问题