# C++ operators:Logical operators

C++ operators are usually some symbols(there are non-symbols operators also) used to perform various operations in our program.Operations like comparing two expression or changing the bits pattern of the data can be performed using operators.Although C++ operators can be classified into many types,here we will discuss the two operators given below:

i)Logical operator and

ii)Bit wise operator.

The logiacl operators will be discussed here the bit wise operators is discussed in another post.There are other operators like mathematical operators,assignment operators,relational operators,etc.these operators are discussed in another post.

#### Logical operator

Logical operators perform operation using the true and false value of an operand or an expression.When logical operator is used the operands are assumed to take the value true or false.The operand can be integer or floating point value.

One way of knowing which operand is true value and which operand false when using integer or float or char as operand is,all the non-zero value:it can be either negative or positive value, is interpreted as true and zero as false.

If an expression is used as an operand the true or false value can be deduced from the expression itself,you will see later how when discussing the various types of logical operator.

Since the resultant value we get is always either true or false when logical operator is use,an expression using logical operator is use for making decision in our program(more detail about this in Chapter 2).In C++ there are three logical operators,each one of them is discuss in detail below.

i)AND denoted by ‘&&‘.

ii)OR denoted by ‘||‘ and

iii)Negation denoted by ‘!‘.

#### i)And or &&

&& will give a resultant value as true only when both the operands or expressions are true otherwise false.Look at the code below.

char c1=’9′ , c2=’Z’ ; int i1=9 , i2=100 ; int result= (c1 < i1) && (i2 < c2) ; cout<< result << endl ; cout<< (2 && 78) << endl /// Gives true << (-23 && 0 ) << endl ///Gives false << (90.789 && -78.5 ) ; ///Gives true

In the expression **(c1 < i1)**: c1(9) is not smaller than i1(9) so ,the expression (c1 < i1) evaluates to false .

We can now predict that the expression (c1 < i1) && (i2 < c2) will be false since the first expression (c1<i1) is false and ‘&&‘ requires that both the operands must be true for the expression to evaluate as true.

#### ii)OR or ||

This operator will give the resultant value of an expression as false only when both the operands are false.An example code is given below.

char c1=’&’ , c2=’?’ ; int i=9; int result=(c1 > c2) || i ; cout<< result ; cout<< (23 || 0 ) << endl //Gives true << ( -34.56 || 0 ) ; //Gives true

In the expression “(c1 > c2) || i“, the expression (c1 > c2) is true because c1(‘&’) value is 38 (from ASCII chart) and c2(‘?’) value is 63(from ASCII chart) and as for i(9) the value is true because the compiler consider every integer value not equal to 0 as true.Hence the value of result is 1.

#### Negation or !

Negation always give the opposite value of what it is.For instance if the expression is true negating it will give false as the resultant value.

float f=2.356 ; int i=2 ; char c=’0′; int result1=!(f>i) , result2=!c ; cout<< result1 << “,” << result2 ;

The output is ,

0

0

I am guessing you know the reason of the output above.