如果 if/else 语句中有 2 个 bool 为真,则调用什么

What is called if 2 bools are true in if/else statements

在正常的 if/else 语句中,例如,如果一个 bool 为真,它会调用该语句 例如:

bool1 = true;
bool2 = false;
bool3 = false;

if(bool1){
    DoSomething();
}else if(bool2){
    DoSomethingElse();
}else{
    DoSomethingHelpful();
}

当然,在这个例子中DoSomething()会被调用。

但是如果 2 或 3 个布尔值等于 true 会怎么样,例如:

bool1 = true;
bool2 = true;
bool3 = false;

if(bool1){
    DoSomething();
}else if(bool2){
    DoSomethingElse();
}else{
    DoSomethingHelpful();
}

调用什么语句?它会是 DoSomething() 因为它是编译器读取的第一条语句吗?或者它只是 return 并且有错误

ifelseifelse也可以看成:

if (bool1) {
  DoSomething();
} else {
    if (bool2) {
        DoSomethingElse();
    } else {
       DoSomethingHelpful();
    }
}

DoSomething()会被调用,因为第一个语句满足条件,所以编译器不会报错,其他条件会被跳过。