Dart Logical Operators
Logical operators are the operators that combine two or more conditions, and accordingly return a Boolean value : true or false.
Assume the value of variable A is 25 and B is 40.
Operator | Description | Example |
&& | And - The operator returns true if and only if all the expressions return true. | (A < 10 && B > 10) is False. |
|| | OR - The operator returns true if at least one of the expressions return true. | (A > 10 || B > 10) is True. |
! | NOT - The operator returns the inverse of the expression’s result. For E.g.: !(17 > 5) returns false | !(A < 10) is True. |
AND and OR Operators ( && and || )
The && operator returns true if and only if both the conditions return true. If either of them is false, the result is also false.
The || operator returns true if any one of the expressions returns true. It returns false if and only if both conditions are false.
X | Y | X && Y | X || Y | !X |
0 | 0 | 0 | 0 | 1 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 0 |
1 | 1 | 1 | 1 | 0 |
Program
Consider the following code in Dart that explains logical operator,
void main( )
{
int a = 25 ;
int b = 15 ;
// Using And Operator ( && )
// where both conditions are true
bool c = a > 10 && b > 10 ;
print( c ) ;
// Using And Operator ( && )
// where both conditions are false
bool d = a > 50 && b < 10 ;
print( d ) ;
// Using Or Operator ( || )
// where only one condition is true
bool e = a > 10 || b < 10 ;
print( e ) ;
// Using Or Operator ( || )
// where both conditions are false
bool f = a > 50 || b < 10 ;
print( f ) ;
// Using Not Operator ( ! )
bool g = !( a > 10 ) ;
print( g ) ;
}
Output
true
false
true
false
false