Java 对于互斥条件,多个“if”语句和“if-else-if”语句是否相同?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29945412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:51:14  来源:igfitidea点击:

Are multiple 'if' statements and 'if-else-if' statements the same for mutually exclusive conditions?

javaoopif-statementconditional-statements

提问by arnav bhartiya

Is there any difference between writing multiple ifstatements and if-else-ifstatements ?

编写多个if语句和if-else-if语句之间有什么区别吗?

When I tried to write a program with multiple ifstatements, It did not give the expected results, But it worked with if-else-if.

当我尝试用多个if语句编写程序时,它没有给出预期的结果,但它与if-else-if.

The conditions were mutually exclusive.

条件是相互排斥的。

采纳答案by Eran

When you write multiple if statements, it's possible that more than one of them will be evaluated to true, since the statements are independent of each other.

当您编写多个 if 语句时,有可能其中不止一个被评估为真,因为这些语句是相互独立的。

When you write a single if else-if else-if ... else statement, only one condition can be evaluated to true (once the first condition that evaluates to true is found, the next else-if conditions are skipped).

当您编写单个 if else-if else-if ... else 语句时,只能将一个条件计算为真(一旦找到计算为真的第一个条件,则跳过下一个 else-if 条件)。

You can make multiple if statements behave like a single if else-if .. else statement if each of the condition blocks breaks out of the block that contains the if statements (for example, by returning from the method or breaking from a loop).

如果每个条件块都脱离包含 if 语句的块(例如,通过从方法返回或从循环中脱离),您可以使多个 if 语句的行为类似于单个 if else-if .. else 语句。

For example :

例如 :

public void foo (int x)
{
    if (x>5) {
        ...
        return;
    }
    if (x>7) {
        ...
        return;
    }
}

Will have the same behavior as :

将具有与以下相同的行为:

public void foo (int x)
{
    if (x>5) {
        ...
    }
    else if (x>7) {
        ...
    }
}

But without the return statements it will have different behavior when x>5 and x>7 are both true.

但是如果没有 return 语句,当 x>5 和 x>7 都为真时,它会有不同的行为。

回答by Nimesh

No both are not same. if statements will check all the conditions. If you will write multiple if statement it will check every condition. If else will check conditions until it is satisfied. Once if/else if is satisfied it will be out of that block.

不,两者不一样。if 语句将检查所有条件。如果您将编写多个 if 语句,它将检查每个条件。If else 将检查条件,直到满足为止。一旦 if/else if 被满足,它将在该块之外。

回答by Virginie

Yes, it makes a difference: see The if-then and if-then-else Statements.

是的,它有所不同:请参阅if-then 和 if-then-else 语句

Furthermore, you can easily test it.

此外,您可以轻松测试它。

Code #1:

代码#1:

    int someValue = 10;

    if(someValue > 0){
        System.out.println("someValue > 0");
    }

    if(someValue > 5){
        System.out.println("someValue > 5");
    }

Will output:

将输出:

someValue > 0
someValue > 5

While code #2:

虽然代码#2:

    int someValue = 10;

    if(someValue > 0){
        System.out.println("someValue > 0");
    }else if(someValue > 5){
        System.out.println("someValue > 5");
    }

Will only output:

只会输出:

someValue > 0

As you can see, code #2 never goes to the second block, as the first statement (someValue > 0) evaluates to true.

如您所见,代码#2 永远不会进入第二个块,因为第一个语句 (someValue > 0) 的计算结果为true

回答by kavi temre

if()
{
stmt..
}
else
{
stmt
}
if()
{
stmt
}
here compiler will check for both the if condition.

In below fragement of code compiler will check the if conditions, as soon as first if condition get true remaining if condition will be bypass.

在下面的代码片段中,编译器将检查 if 条件,一旦第一个 if 条件为真,剩下的 if 条件将被绕过。

        if(){

        }
        else if
        {

        }
        else if
        {

        }
        else if
        {

        }