Logical Operators
Logical operators in c language:-
- ! ( logical negation - NOT )
- && ( logical AND )
- | | (logical OR)
An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false.
1. && ( logical AND ):-- If both operands are true or non zero, condition become true or 1. Otherwise condition become false or 0.
Suppose A and B are operands,
If A=2 and B=5, A && B is true or 1.
If A=2 and B=2, A && B is true or 1.
If A=2 and B=0, A && B is false or 0.
If A=2 and B=5, A>0 && B<0 is false or 0.
If A=2 and B=5, A>0 && B>0 is true or 1.
Example:-
#include<stdio.h>
#include<stdlib.h>
int main()
{
int A = 2;
int B = 8;
printf("result is %d. \n",A<0 && B>0);
printf("result is %d.",A>0 && B>0);
}
Output:-
result is 0.
result is 1.
2. ! ( logical negation - NOT )
- If the logical condition is true or operands are none zero, then logical NOT operator will make it false or Zero.
- If logical condition is false or operands are zero, then logical NOT operator will make it true or 1.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int A = 0;
int B = 8;
printf("result is %d\n",!A);
printf("result is %d",!B);
}
Output:
result is 1
result is 0
3. | | (logical OR) :
- If both operands are false or zero, condition become false or 0. Otherwise condition become true or 1.
Suppose A and B are operands,
If A=2 and B=5, A || B is true or 1.
If A=2 and B=0, A || B is true or 1.
If A=0 and B=0, A || B is false or 0.
If A=2 and B=5, A<0 || B<0 is false or 0.
If A=2 and B=5, A>0 || B>0 is true or 1.
Example:-
#include<stdio.h>
#include<stdlib.h>
int main()
{
int A = 2;
int B = 8;
printf("result is %d. \n",A<0 || B<0);
printf("result is %d.",A>0 || B>0);
}
Output:-
result is 0.
result is 1.
Comments
Post a Comment