Operator
Meaning
&&
And
|| Or
! Not
Logical operators are used to
compare & evaluate logical and relational expressions. Operator &&
is referred as logic and, the operator || is referred as logic or. The result
of a logic and operation will be true only if both operands are true where as the
result of a logic or operation will be true if either operand is true or if
both operands are true. In other word, the result of a logic or operation will
be false only if both operands are false.
Suppose that i is an integer variable whose
value is 7, f is a floating-point variable whose value is 5.5 and c is a
character variable that represents the character ‘w’. Several complex logical
expressions that make use of these variables are shown below:
Expression Interpretation
Value
(i>=6) && (c= = ‘w’) true 1
(i>=6) || (c = = 119) true 1
(f<11) && (i>100) false 0
(c! = ‘p’) || (iff<=10) true 1
!(i>(f+1)) false
0
C also includes the unary
operator ! that negates the value of a logical expression, i.e. it causes an
expression that is originally true to become false and vice-versa. This
operator is referred to as the logical negative (or logical not) operator.
Comments
Post a Comment