Conditional statement true in both parts of if-else-if ladder

后端 未结 4 1807
情话喂你
情话喂你 2020-12-10 09:02

If you have code like this:

if (A > X && B > Y)
{
   Action1();
}
else if(A > X || B > Y)
{
   Action2();
}

With

4条回答
  •  不知归路
    2020-12-10 09:10

    If both A > X and B > Y are true then your code will only execute Action1. If one of the conditions is true it will execute Action2. If none are true, it will do nothing.

    Using this:

    if (A > X || B > Y) {
       Action2
       if (A > X && B > Y) {
          Action1                                    
       } 
    }
    

    will result in the possibility of both actions occurring when A > X and B > Y are both true.

提交回复
热议问题