C Programming

Logical Operators

Posted on 02nd May 2013

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:

OperatorMeaning
&&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

Example

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.

ExpressionMeaningResult
(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

Comments

Nothing yet..be the first to share wisdom.