Logical operators

An operator can be defined as a symbol that is used for performing different operations. In a programming language, there are various types of operators such as arithmetic operators, relational operators, logical operators, assignment operator, increment/decrement operators, conditional operators, bitwise operators, and shift operators.

In this article, we are discussing the logical operators and their types, along with an example of each. Here, we will discuss all types of logical operators irrespective of a particular programming language. Some programming languages support limited operators, so some of the logical operators that we are discussing may or may not be supported by the programming language you are using.

Logical operators are generally used for combining two or more relational statements. They return Boolean values. The logical operators are used primarily in the expression evaluation to make a decision. These operators allow the evaluation and manipulation of specific bits within the integer.

The types of Logical operators with their description are tabulated as follows -

OperatorsDescription
&& (Logical AND)This operator returns true if all relational statements combined with && are true, else it returns false.
|| (Logical OR)This operator returns true if at least one of the relational statements combined with || is true, else it returns false.
! (logical NOT)It returns the inverse of the statement's result.
xor (Logical XOR operator)This operator returns true if either statement one is true or statement two is true but not both.
and (Logical AND)This operator returns true if all relational statements combined with it are true, else it returns false.
or (Logical OR)This operator returns true if atleast one of the relational statements combined with it is true, else it returns false.

Let's discuss each type with an example.

&& (Logical AND)

The logical && operator doesn't check the second condition if first condition is false. It checks the second condition only if the first one is true. The evaluation in logical && expression is left to right. With the help of other Logical operators, we can convert Logical AND to Logical OR. This operator accepts two operands.

Suppose we have two conditions A and B. So, we can see the possible values of A && B in the below table.

ABA && B
TrueTrueTrue
FalseTrueFalse
TrueFalseFalse
FalseFalseFalse

Example:

Let's understand the concept of logical && operator with a basic example.

Here, we are discussing the four cases to show the possible conditions of && operator and also to check the result where it returns false or where it returns true. In every case, we are not using the parentheses as it is not technically required. Programmers often use it to make the logic of expression very clear.

Case 1:

Statement 1 in the above line is 10 > 20 and statement 2 is 20 > 5. It is clear that the statement1 is false, this simply means that the operator will not further check the second condition whether it is true or not. AND operator will give the true value only when both statements are true. So, we will get false.

Case 2:

In this case, operator will again return false as a result. Statement 1 in the above line is true, so in this case, the operator will further check the second statement, but the second statement is false so, the operator will give us the false as a result.

Case 3:

In this case, both statements are false. So, the operator will give us the false as a result. Although it is similar to the first case, the operator will not check the second statement because the first statement is not true.

Case 4:

In this case, we can see both statements are true, so the operator will give us the true as a result.

|| (Logical OR)

The Logical OR operator is represented with the two vertical line symbols. It works opposite to the logical AND operator, as it only gives false when both statements of the expression are not true. Otherwise, it returns true.

The logical || operator doesn't check the second condition if first condition is true. It checks the second condition only if the first one is false.

The logical || operator accepts two operands. Suppose we have two conditions A and B. We can see the possible values of A || B in the following table.

ABA || B
TrueTrueTrue
FalseTrueTrue
TrueFalseTrue
FalseFalseFalse

Example

Let's understand the concept of logical || operator with a basic example.

Here, we are discussing the four cases to show the possible conditions of || operator and also to check the result where it returns false or where it returns true.

Case 1:

Statement 1 in the above line is 10 < 20 and statement 2 is 20 < 5. It is clear that statement 1 is true, this simply means that the operator will not further check the second condition whether it is true or not. As stated earlier, OR operator will give the true value when one of the statements of an expression is true.

Case 2:

In this case, operator will again return true as a result. Statement 1 in the above line is false, so in this case, the operator will check the second statement, and as the statement2 is true, the operator will return true as a result.

Case 3:

In this, both statements are true, so the operator will give us the true as a result. The first statement is true, and after checking it, the operator will not check the second statement and gives us the true as a result.

Case 4:

Here, both of the statements are false. So, after checking the first statement operator will check the second statement because the first statement is false, and because the second statement is also wrong, we will get the false as a result.

! (Logical NOT operator)

This logical operator is represented as an exclamation sign (!). This operator accepts a single argument and returns the inverse value of the corresponding argument.

Suppose we have an operand A. So, the Logical NOT operator will return the opposite possible values of A.

A!A
TrueFalse
FalseTrue

If the value of argument is false, NOT operator will return true, and vice versa.

Example

Case 1:

In this case, the value of argument is false, but as we are using the NOT operator, we will get the true as a result. This is because the NOT operator will inverse the value of the argument.

Case 2:

In this case, we will get the false as the result because the value of argument is true.

xor (Logical XOR operator)

XOR stands for "Exclusive-OR". The "xor" keyword is a logical operator that is supported in PHP. It returns true when either statemen1 is true, or statement2 is true, but not both. In simple words, we can say that the xor operator will return true only when one of the statements is true and another one is false. If both of the statements are true, the xor operator will return false.

The xor operator takes two operands and returns false when both operands have the same value.

NOTE: Sometimes, this operator leads to confusing results as its precedence is lower than the assignment operator. So, In order to avoid unexpected results, one should have to wrap the expression in parentheses.

Suppose we have two conditions A and B. So, we can see the possible values of A xor B.

ABA xor B
TrueTrueFalse
FalseTrueTrue
TrueFalseTrue
FalseFalseFalse

and operator

This operator works similar to the && operator. Although both 'and' and '&&' works similar, the difference is of precedence. Precedence decides which operation should be performed first. The precedence of the '&&' operator is higher than the precedence of the '=' operator, whereas the precedence of 'and' is lower than the '=' operator.

It is supported in php. So, let's understand the difference between 'and' and '&&' by using an example in php. In the following code, we are using both operators in the same condition. So, let's see the results, whether they are the same or not. Although both operators work similar, the result might be different due to their precedence.

Output

Logical operators

The output is different on using 'and' and '&&' operators on the same condition. Let's understand the reason behind this.

As we stated earlier, the precedence of logical and is lower than the assignment (=) operator. So, in the statement $res = TRUE and FALSE; first, the value True assigned to the variable $res due to the higher precedence of the assignment operator, and then the operation using AND operator will perform internally, but now there is no assignment, so the $res still holds true and return true as a result. This simply means that the statement $res = TRUE and FALSE; will be interpreted as (($res = TRUE) and FALSE); so, the value of $res will be true.

On the other hand, in the statement $res = TRUE && FALSE; the precedence of && is higher than the assignment (=) operator, so first the logical && operation is performed, and then the result will be assigned to the corresponding variable. That's why the statement using the && is showing the exact result. Here, the statement $res = TRUE && FALSE; will be interpreted as ($res = (TRUE && FALSE)); so, the value of $res will be false.

So, in order to explain, the fundamental difference between both operators is of their precedence, but basically, both perform the same operation.

or operator

This operator works similar to the || operator. Although both 'or' and 'II' works similar, the difference is of precedence. Precedence decides which operation should be performed first. The precedence of the 'II' operator is higher than the precedence of the '=' operator, and the precedence of 'or' is lower than the '=' operator.

It is supported in PHP. So, let's understand the difference between 'or' and '||' by using an example in PHP. In the following code, we are applying both operators on the same condition. So, let's see the results, whether they are same or not. Although both operators work similar, the result might be different due to their precedence.

Output

Logical operators

The output is different on using 'or' and '||' operators on the same condition. Let's understand the reason behind this.

As we stated earlier, the precedence of logical or is lower than the assignment (=) operator. So, in the statement $res = FALSE or TRUE; first, the value FALSE assigned to the variable $res due to the higher precedence of the assignment operator, and then the operation using or operator will perform internally, but now there is no assignment, the $res still holds FALSE and return FALSE as a result. This simply means that the statement $res = FALSE or TRUE; will be interpreted as (($res = FALSE) or TRUE); so, the value of $res will be false.

On the other hand, in the statement $res = FALSE || TRUE; the precedence of || is higher than the assignment (=) operator, so first the logical || operation is performed, and then the result will be assigned to the corresponding variable. That's why the statement using the || is showing the exact result. Here, the statement $res = FALSE || TRUE; will be interpreted as ($res = (FALSE || TRUE)); so, the value of $res will be true.

In order to explain, the fundamental difference between both operators is of their precedence, but basically, both perform the same operation.






Latest Courses