Logical operators are used to perform logical operations on the given two variables.
Logical operators :
Operator | Usage |
---|---|
&& | expr1 && expr2 |
|| | expr1 || expr2 |
! | !expr1 |
Logical operator chart :
Operator | Condition 1 | Condition 2 | Result |
---|---|---|---|
&& | True | True | True |
True | False | False | |
False | True | False | |
False | False | False | |
|| | True | True | True |
True | False | True | |
False | True | True | |
False | False | False | |
! | True | - | False |
False | - | True |
Example :
a = 5;
b = 10;
(a == 5) && (b < 5) = false
/*
here, first expression a == 5 is true
second expression b < 5 is false
so, final result of && operator is false
(True && False = False)
*/
(a == 4) && (b < 15) = false
(a == 5) && (b < 15) = true
(a == 4) && (b < 5) = false
(a == 5) || (b < 5) = true
(a == 4) || (b < 15) = true
(a == 4) || (b < 5) = false
!(a == 5) = false
!(a == 4) = true
0 Comments