Logical operators in c

Logical operators in c

When an Expression that combines two or more expressions is know as logical expression. To combining these expressions we use logical operators.This logical operator return 0 for false and 1 for true. The operands may be constant,expressions or variables . In c language there are three logical operator –

Operator Name
&& AND
|| OR
! NOT

Here in this example NOT is a unary operator while the others two OR, AND are binary operators. Before using These operators let us understand the concept of these operators of true and false. in c language any non zero value regarded as true and the zero is regarded as false.

AND (&&) operator

This operators gives the net result true if both condition will be true, Otherwise result will be false. Let us see Example

Boolean Table
Condition1 Condition2 Result
True True True
True False False
False False False
False True False

Let us understand the concept of AND operator. Let us take three variables a=15, b=6,c=0 let us take expression and results

Expression Result Value of Expression
(a==18)&&(c>b) false && false false 0
(a==15)&&(a>b) true && true true 1
(a>b)&&(b>c) true && true true 1
a&&c true && false false 0

in the last row example the expression a&&c we have take only variables a and c. since non zero value is regarded as true and zero value is regarded as false, so in this example variable a considered as true and variable c is considered as false.

OR (||) Operator

This OR operator gives the result false. if both conditions have the value false. otherwise the result is true

Boolean Table
Condition1 Condition2 Result
True True True
True False False
False False False
False True False

Let us understand the concept of OR operator. so take three variables a=20, b=15, c=0;

Expression Result Value of Expression
a || b true || true true 1
a || c true || false false 0
(a<5 || (c>b) false || false false 0
(a!=b) || c true || false false 0

NOT (!) Operator

This is a Unary operator and its negates the values of any condition. So if the value of the condition is false then it gives the result true. And the value of condition is true then it gives the result false. So now understand the concept of NOT operator.

Boolean Table
Condition Result
True false
false true

Let us understand the concept of NOT operator

Expression Result Value of Expression
!a !true false 0
!c !false true 1
!(a>b) !true false 0
!(b&&c) !false true 1