The Logical Operators
The logical operators are primarily used to control the program flow. The concept of logical operators is to allow programs to make decisions based on multiple terms and conditions. Each of the operands is considerably conditioning to determine the overall value. The logical operators have the symbol of two vertical lines (l l).
The logical operators do the following:
- It evaluates the operands starting from the left part to the right.
- In each operand converts into Boolean. If the result is true, it stops and reverts to the original value.
- All the operands resulted in false will revert to the last operand.
Values will return to its normal form without the conversion. In other words, the chain of logical operators returns the first true value or the last one if no true value found.
The AND operator has the symbol of two ampersands (&&). In classical programming, AND returns true If both operands are truthy and false.
The AND (&&) operator does the following:
- Evaluates the operands from the left part to the right
- In each operand, convert it to Boolean. If the result is false, it stops and returns the original value of the operand.
- All the operands evaluated truthy will return the last operand.
In other words, AND will return the first falsy value or the last value if found none. The rules are just similar to OR. The difference is that, AND will return the falsy value while OR returns the first truthy value. When all the values are truthy, then the last value will return.
This logical operator has the symbol of an exclamation sign (!). The operator only accepts a single argument and only does the following:
- Converts the operand to Boolean type: true/false returns the inverse value
Note that a double not (!!) means to convert an amount into Boolean type. The precedence of NOT (!) is the highest of all logical operators. It is always to execute first following by && or I I.
Logical expressions can only have the values of true and false. There are relational operators used to structure rational expressions. Logical operators are used to comparing two values having the same type. the table below will show the relational operators along with their meanings.
Operator / Meaning
Not equal to
Less than or equal to
Greater than or equal to
Some logical operators only used between complete logical expressions. The table below will show the logical operators along with the meanings:
Operator / Meaning
Changes the value of the expression into the opposite value
TRUE only if both the logical expressions are TRUE
TRUE if either the logical expression is TRUE
TRUS if only one expression is true
TRUE if the expression has the same truth value
TRUE if the expression does not have the same value
Each of the logical operators has unique characteristics. All have a vital role in the programming field. It is essential to understand and distinguish each.