Logical Operators

Logical Operators
C में तीन logical ऑपरेटर है ANDOR और NOT और इनको क्रमशः &&|| और ! से प्रदर्शित करते है।

AND and OR Operators
Logical Operators AND and OR combine the result of two relational expressions and give the final result as true or false.   
AND and OR both take two expressions, one on left and other on right. This means these are binary operators. The general forms of using these two operators are –

Expression1 && Expression2
Expression1 || Expression2

Here, operator must be && (AND) or || (OR) and Expression1 and Expression2 must be relational expressions.  The AND operator gives result true when both expressions evaluates to true otherwise it gives the result false. The OR operator gives true result when at least one expression is true. It gives result false when both expressions evaluates to false. Here is the table that describes the working of AND and OR operators.



AND
OR
Expression1    
Expression2      
Expression1 && Expression2
Expression1 || Expression2
true
true
True
true
true
false
False
true
false
true
False
true
false
false
False
false



Some Examples will clear the working of && and || –
Let x = 10, y = 20 and z = 30. Then
x < y is true. Because x’s value 10 is less than y’s value 20.
x > y is false. Because x’s value 10 is not greater than y’s value 20.
y < z is true. Because y’s value 20 is less than z’s value 30.
y > z is false. Because y’s value 20 is not greater than z’s value 30.
Now, see the working of && and || operators.
x < y && y < z will result true. Because both x < y and y < z, are true.
x > y && y < z will result false. Because x > y is false. Remember, && requires both conditions true.
x < y && y > z will result false. Because y > z is false.
x > y && y > z will result false. Because x > y and y > z both are false.

x < y || y < z will result true. Because x < y is true and y < z is true.
x > y || y < z will result true. Because y < z is true. Remember, || requires only one condition true.
x < y || > z will result true. Because x < y is true.
x > y || y > z will result false. Because x > y and y > z both are false.

Here are some important things about && and || which we would like to tell you –
In case of &&, the second expression is not evaluated if first one is false. For example, in x > y && y < z,
y < z will not be evaluated if x > y is false. This is obvious because if x > y is false, no matter what y < z evaluates to, the result will be false. Recall && requires both expressions true (see table).

In case of ||, the second expression is not evaluated when first one is true. For example, in x > y || y < z,    y < z will not be evaluated if x > y is true. This is obvious because if x > y is true, no matter what y < z evaluates to, the result will be true. Recall || requires only one expression true (see table).
  
NOT Operator
Not Operator represented by !. It reverses the result of a relational expression. That is, it converts true to false and false to true. NOT is a unary operator. The general form of using NOT operator is –
! Expression
If Expression evaluates to true, NOT gives the result false. If Expression evaluates to false, NOT gives the result true. Here is the table to make it clear.



NOT
Expression
! Expression
true
false
false
true



The NOT operator always converts 0 to 1 and all non – zero values to 0. For example, !0 evaluates to 1 and !1, !5, !-4, all evaluates to 0.     

It should be noted that NOT operator has higher precedence than arithmetic and other two logical operators AND and OR. So you should be careful while using this. Here is an example –
Suppose, x = 5 and y = 4. Consider the expression  ! x+y <= 3. This is written assuming that NOT will reverse the result of the expression x+y <= 3. But this is not what that will happen. As just described, NOT has higher precedence over both arithmetic and logical operators, so it will be grouped first, then + is grouped and then the comparison <= will be made. The expression after overall grouping is ((! x)+y) <= 3. This gives ((! 5)+4) <= 3 means ((0)+4) <= 3 that is 4 <= 3 which is false. If the same expression is solved how it was assumed to be solved then you will see that the result is true. Mean to say,  solve ! x+y <= 3  as ! 5+4 <= 3 then ! 9 <= 3 then ! false means true. You can easily see that the assumed result is “true” but the actual result is “false”. To obtain the result what was assumed, the expression ! x+y <= 3 must be written as ! (x+y >= 3) this will give ! (5+4 <= 3) then ! (9 <= 3) and then ! (false) means true, which is same as the assumed result. The same method is applicable anywhere where you use NOT operator.

Also note that no parenthesis are shown in the general form of NOT but you should care where to use them
  Also show the example of ! 4 > 3.
Logical Expressions and Precedence and Associativity of Logical Operators
An expression, in which a logical operator is present, is termed as logical expression. A logical operator, such as && or ||, gives the result by combining the result of two or more relational expressions. These relational expressions may contain simple / complex arithmetic expression. It means in relational expressions, arithmetic expressions are compared and the result is given as true or false. Then logical operators (&& or || )  combines the results of two or more relational expressions and give the final result as true or false.  
For example, if a, b, c, and d are variables then  –
            a + b, c + d , p * q and r * s                are  arithmetic expressions.
            a + b > c + d  and  p * q  > r * s          are relational expressions in which arithmetic
expressions are present on each side of each relational              operator.
            a + b > c + d  &&  p * q  > r * s         is a logical expression that contains relational expressions.
            a + b > c + d  ||  p * q  > r * s              is a logical expression.

Show How a + b >c && b + c > a && c + a > b is solved

Also show how a + b >c || b + c > a || c + a > b is solved
  






1 comment:

  1. सी प्रोग्रामिंग नमूना कोड प्रोग्रामर की मदद करने के लिए
    प्रिंट चरित्र इतिहास c नमूना कोड

    ReplyDelete