我想确定一些事情,
当我有多个 if/else
条件和if
并不意味着 else
其中之一的条件 if
状况;做下一个 else
声明适用于最后if
声明?
例如:
if(condition1)
{
//operation1
}
if(condition2)
{
//operation2
}
else
{
//operation3.
}
像上面的例子,如果我不使用 else
对于第一个if
声明,其中 if
声明这样做 else
为。。。工作?如果我不指定 else
,这会对我造成问题吗?对于每个 if
?
我做了一些测试,但想确定它实际上是如何工作的。
最佳答案
An else is associated with the lexically nearest preceding if that is allowed by the syntax.
因此,在您的示例中,else
属于第二个 if
(具有 condition2
的那个)。
关于c - else 语句遵循什么顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35699053/