在C编程语言中,级联if语句和嵌套if语句之间有什么区别?
答案 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
是真还是假。