As studied earlier, Operators are the symbols, which performs operations on various data items known as operands. For Example: in a a+b
, a
and b
are operands and +
is an operator.
Note:- To perform an operation, operators and operands are combined together forming an expression. For example, to perform an addition operation on operands a and b, the addition(+) operator is combined with the operands a and b forming an expression.
Logical operator is used to find out the relationship between two or more relationship expressions.
The C language logical operators allow a programmer to combine simple relational expression to form complex expressions by using logical not, and and or.
Operator | Description | Example |
---|---|---|
&& | Logical AND operator. If both the operands are non-zero, then the condition becomes true. | (A && B) is false. |
|| | Logical OROperator. If any of the two operands is non-zero, then the condition becomes true. | (A || B) is true. |
! | Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. | !(A && B) is true. |
Taking the logical And, when two expressions are added the resulting expression will be true only if both of the sub-expressions are true. Eg, (X>5) && (Y<15) will be only true if x greater than 5 and if y is less than 15. The second expression (Y,15) will not be evaluated if the first expression is false.
A logical expression is evaluated from left to right as far as needed to determine the logical result. Eg. in the expression (Y<15)&&(X++>15), the value of X is incremented only if the value of Y is less than 15.
The logical or is represented by two vertical bars between two other expression as expression1 || expression2. The logical result of the entire expression will be true if either of the two expression surrounding the || is logically true. Expression involving the logical or are evaluated from left to right only when the result of the logical expression is known.
Line 1 - Condition is true Line 2 - Condition is true Line 3 - Condition is not true Line 4 - Condition is true