C编程语言中的级联和嵌套if语句

时间:2016-04-19 17:00:46

标签: c if-statement

在C编程语言中,级联if语句和嵌套if语句之间有什么区别?

1 个答案:

答案 0 :(得分:3)

级联:

if (condition1)
{
    // do one thing
}
if (condition2)
{
    // do other thing
}

此处,如果condition1为真,则one thing将完成。同样,如果condition2为真,则other thing也将完成。

嵌套:

if (condition1)
{
    // do one thing
    if (condition2)
    {
        // do other thing
    }
}

此处,如果condition1为真,则one thing将完成。而且,如果condition2 为真,那么other thing也会完成。

请注意,在后一种情况下,两个条件都必须为true才能发生other thing。在第一种情况下,如果other thing为真,则condition2会发生,而不管condition1是真还是假。