Logical Operators

Logical operators operates on Boolean expressions or values. The result of that operation will be a Boolean result (True or False). Logical operators in C are:

&&AND - Result TRUE if Left side and right side operands are both TRUE
||OR - Result TRUE if either Left side or right side operand is True or Both operands are True
!NOT - Invert the boolean expression or value. True will change to False and False to True


Consider three variable a, b and c as below

a = 10;
b = 15;
c = 10;

The table below shows some expressions using above variables and the results.

(a = c) && (a < b)a = c is True; a < b is True - Both expressions are TRUETRUE
(a = c) || (a > b)a = c is True; a > b is False - One expression is TRUETRUE
!( a > b) a > b is False; Result is Inverse of FalseTRUE
( a > b) && (c < b)a > b is False, c < b is True; Left side is FALSEFALSE
( a > b) && (c > b)a > b is False, c > b is False; Both FALSEFALSE
!( a = c) a = c is True; Result is inverse of TrueFALSE

Post a comment


Your Comment

Email (We dont publish it)


Nothing yet..be the first to share wisdom.