Saturday 7 January 2012

Logical Operators

There are three logical operators in C language, they are,or,and,not. They are represented by &&, ||,! respectively.
These operators are refered to as logical and, logical or,logical not respectively.
The result of a logical and operation will be true only if both operands are true, whereas the result of a logical or operation will be true if either operand is true or if both operands are true.
The logical operators act upon operands that are themselves logical expressions.
(i) Logical AND (&&):-
It is a binary operator.
Syntax: op1 && op2
Table
op1         op2      op1&&op2
0               0            0
0               1            0
1               0            0
1               1            1
C language treats a non-zero value as true and 0 as false,But gives true as 1 and false as 0.
Example
main()
{
int a=7,b=13,C;
C=a&&b;
printf("%d",c);
}

(i) Logical OR (||):-
It is a binary operator.
Syntax: op1 && op2
Table
op1         op2      op1&&op2
0               0            0
0               1            1
1               0            1
1               1            1
C language treats a non-zero value as true and 0 as false,But gives true as 1 and false as 0.
It changes true and false to true.
Example
main()
{
int a=7,b=0,C;
C=a||b;
printf("%d",c);
}

i) Logical NOT (||):-(Negation Operator)
It is a unary operator.
Syntax: op1 && op2
Table
op         !op
1             0
0             1
C language treats a non-zero value as true and 0 as false,But gives true as 1 and false as 0.
It changes true to false and false to true.
Example
main()
{
int a=7,b;
b=!a|;
printf("%d",b);
}

No comments:

Post a Comment

host gator coupon