Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I simplify this if-else into one statement?

How can I simplify the following code to one line (without the if-else statement)?

public static void main(String[] args) 
{
    boolean result;
    boolean a = false;
    boolean b = false;
    boolean isEmpty = false;
    if (a) {
        result = isEmpty && !b;
        System.out.println("if  " + (isEmpty && !b));
    } else {
        System.out.println("else    " + !b);
        result = !b;
    }
    System.out.println(result);
}
like image 428
S Fasda Avatar asked Nov 30 '25 11:11

S Fasda


1 Answers

If a is true, you must check that isEmpty AND !b are both true. If a is false (or !a is true), it's enough to check that !b is true.

Therefore you can replace the logic of the if statement with:

result = !b && (isEmpty || !a);
like image 76
Eran Avatar answered Dec 02 '25 00:12

Eran



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!