Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way to handle combination of two bool conditions?

I have method with two parameters: bool1 and bool2. Both are boolean of course. I have to handle in code every combination of these conditions. Is any better way to do it then using nested if / else :

if (bool1)
{
    if(bool2)
    {
    }
    else
    {
    }
}
else
{
    if(bool2)
    {
    }
    else
    {
    }
}
like image 210
Kamil Będkowski Avatar asked Jan 20 '26 21:01

Kamil Będkowski


2 Answers

if (bool1 && bool2) { }
else if (bool1) {}
else if (bool2) {}
else {}
like image 112
Neo Avatar answered Jan 22 '26 12:01

Neo


var bothAreTrue = bool1 && bool2;

if(bothAreTrue){

}else if(bool1){

}else if(bool2){

}else{ //none is true

}
like image 25
cederlof Avatar answered Jan 22 '26 12:01

cederlof



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!