Table of Contents
Introduction
Logical operators are mainly used to check if the result of an expression is true or false. We have 3 logical operators which help us in better decision-making.
Logical Operators In Salesforce
Operator | Name | Syntax | Description |
---|---|---|---|
&& | Logical AND | expression1 && expression2 | returns true only if both expression 1 and expression 2 are true |
|| | Logical OR | expression1 || expression2 | returns true if any one of the expressions either expression 1 or expression 2 is true |
! | Logical NOT | !expression | returns true if the expression is false and false if the expression is true |
Logical AND operator
System.debug((6 > 3) && (8 > 6)); // true
System.debug((6 > 3) && (8 < 6)); // false
Explanation
Expression1 (6>3) is true and Expression2 (8>6) is also true and here we have a logical AND operator in which if both the conditions or expressions are true, the result would be true. Hence, the output is true.
Logical OR operator
System.debug((6 < 3) || (9 > 5)); // true
System.debug((7 < 3) || (7 < 5)); // false
System.debug((8 > 3) || (7 < 5)); // true
Explanation
Expression1 (6<3) is false and Expression2 (9>5) is true. Since we are using a logical OR operator here in which result would be true if any of the expressions is true.
Hence, the output is true.
Logical NOT operator
System.debug(!(9 == 3)); // true
System.debug(!(9 > 3)); // false
Explanation
Expression (9 == 3) is not true and here we are using the NOT operator which returns false if the expression is true and vice versa.
Hence, the output is true.
0 Comments