Just like arithmetic operations follow the BODMAS rule, similarly, there is a predefined precedence order in which a statement with logical operators is evaluated.
Let us try to understand the precedence order through the following video:
So now consider a case where you have three booleans like this boolean a is true, B is false and C is true. Now in the case where you are trying to evaluate an expression like this that is A or B and not of C, then what do you think will be the result of this operation? So type this in your file and try to run it and see what output do you get on the browser console. Then write that output in the question that follows. So now if you try to evaluate this expression simply from left to right what you will see is that A or with B that is true or with false. So this is going to give me true. Now if I do true and of not C. So here not of C is actually false since C is true. So then if I do true and with false so the and between two values such that if either one of them is false gives me false. So on evaluating the expression simply from left to right gives me the value false. But you might have noticed that when you run the same expression then the result on the console actually gives you value true.
So this means that the expressions like these are not actually evaluated from left to right and they follow a precedence order between these different logical operators. So this precedence order actually tells you that first all the not logical operators will be resolved, then comes the and operator and lastly comes the or operator. So in any given expression first not symbol will be evaluated, then the expression with the and symbol will be evaluated and lastly the expression with the or symbol will be evaluated. So based on this precedence order can we now evaluate this expression? So basically we had A or B and not of C. So according to this not is the first in the precedence order. So first not of C will be evaluated, not of C will give me false. Then the and operator comes. So we say B and with false what is B here? B here is false so it gives me false here and when I try to do the and between two false values it gives me a result false. Then in the last step I will do a or with this false value what is A here? A here is true. So the or operation between a true value and a false value gives me true. So basically a here is true and when I do true or with false it gives me the result true. So now you have concluded how this true came out to be printed on your console. Now suppose you encounter a situation where you write a statement like this but you don't want the and operator to be evaluated before the or operator. Then if you want a certain expression to be evaluated first, then you can simply enclose it within the brackets. As an example, suppose you had something like this so according to this not will be evaluated first then and then or but suppose you want that or should be evaluated before and then you can simply enclose this expression within the brackets. So then what will happen is that this or operation will be evaluated before that and operation. So this is what is the precedence order between these logical operators.
Logical operators and their precedence order explained
Example with boolean values of A, B, and C
Explanation of left-to-right evaluation of expression
Not operator evaluated first, then and operator, and lastly or operator
Explanation of how to use parentheses to alter evaluation order
Importance of understanding logical operator precedence for correct evaluation of expressions
As you learned, this is the precedence order in which the logical operations are evaluated:
NOT
AND
OR
If you want an expression to be evaluated first, then you need to enclose it within brackets.
Let us now try to code all this on the IntelliJ console.
You can download the following java file which will be very useful to you while watching the video. Right click on the java file and open it with Intellij. Please be careful with the name of the java file. The name of the java file should exactly be same as the class name inside it for the code to run. So make changes accordingly.
Since you have now learned to write a lot of code, in the following segments, you will be introduced to some of the best practices to be followed while coding.