Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How a given if condition is giving true in java

I am unable to understand why following if blocks execute. How the if conditions will be evaluated?

public class Test
{
    public static void main(String[] args)
    {
        if (true || (false ||  true) && false)
        {
            System.out.println("How does this condition becomes true.");
        }

        if (false && (false ||  true) || true)
        {
            System.out.println("Same with this condition, why is it true.");
        }
    }
}
like image 615
Rahul khandelwal Avatar asked Jul 20 '18 10:07

Rahul khandelwal


People also ask

How do you write an if statement with condition?

OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False) NOT – =IF(NOT(Something is True), Value if True, Value if False)

How are if conditions evaluated in Java?

Java if StatementThe if keyword is used to check if a condition is true or not. If it is true, then the specified code inside the curly braces are executed. Note: The condition inside the parentheses must be a boolean expression. That is, the result of the expression must either evaluate to true or false.

How do you find the boolean value in an if statement?

An if statement checks a boolean value and only executes a block of code if that value is true . To write an if statement, write the keyword if , then inside parentheses () insert a boolean value, and then in curly brackets {} write the code that should only execute when that value is true .


2 Answers

First of all, you must already know false || true is true.

Apart from that, you need to know the other two things:

  • short circuiting.

The && and || operators "short-circuit", meaning they don't evaluate the right hand side if it isn't necessary.

  • Operators and the annoying precedence

When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.

From the precedence table, we know && is higher than ||.

So in your question, let's do the basic reasoning.

if (true || (false || true) && false)

true || no_matter_what_is_in_right_side will be true, so we got a true.

if (false && (false || true) || true)

This one is trickier than the previous, this time we have to consider the annoying precedence. We do the reasoning as:

false && no_matter_what_is_in_right_side will be false and all we have left is:

false || true

Again we have true in the end.

But if you already noticed the trick: we start from the rightmost true then we can do the reasoning as the previous one directly get the true.

As Michael mentioned, we'd better use parenthesis to express the exact order instead of relying on the precedence, which is actually hard to remember ##.

like image 154
Hearen Avatar answered Oct 12 '22 22:10

Hearen


&& has higher precedence than || (source). So adding in parens to emphasize how those are being evaluated:

    if (true || ((false ||  true) && false))
// -------------^-------------------------^

and

    if ((false && (false ||  true)) || true)
// -----^-------------------------^

As you can see, you end up with x || y where x is true in the first example, and y is true in the second example. Naturally both true || false and false || true are true.

You can see it in action by using a method that outputs what it's doing (live copy) (remember that both || and && short-circuit, meaning they don't evaluate operands that cannot change the result):

public class Test
{
    private static boolean b(String label, boolean value) {
        System.out.println(label + ": " + value);
        return value;
    }
    public static void main(String[] args)
    {
        if (b("1", true) || (b("2", false) || b("3", true)) && b("4", false))
        {
            System.out.println("How does this condition becomes true.");
        }

        if (b("5", false) && (b("6", false) || b("7", true)) || b("8", true))
        {
            System.out.println("Same with this condition, why is it true.");
        }
    }
}

When run, that outputs:

1: true
How does this condition becomes true.
5: false
8: true
Same with this condition, why is it true.
like image 29
T.J. Crowder Avatar answered Oct 12 '22 21:10

T.J. Crowder