Logical NOT Operator
Dart Logical NOT Operator takes a boolean value as operand (right side) and returns the result of logical NOT gate operation.
Logical NOT Operator returns true if the operand is false, or false if the operand is true. Therefore, logical NOT operator is used to check if the condition is not true.
Symbol
!
symbol is used for Logical NOT Operator.
Syntax
The syntax for Logical OR Operator is
! operand
where operand
is a boolean value or expression that evaluates to a boolean value.
Truth Table
The truth table of NOT operation is shown in the following.
operand | ! operand |
---|---|
true | false |
false | true |
Example
Check if given number is not negative
In the following example, we check if the given integer number n
is not negative. Let us first consider the condition of n being negative n < 0
. And the condition to check if n is not negative would be !(n < 0)
, using logical NOT operator.
main.dart
import 'dart:io';
void main(){
print('Enter n');
var n = int.parse(stdin.readLineSync()!);
if ( !(n < 0) ) {
print('$n is not negative.');
} else {
print('$n is negative.');
}
}
Output
Enter n
58
58 is not negative.
Output
Enter n
-25
-25 is negative.
Conclusion
In this Dart Tutorial, we learned about Logical NOT Operator, its syntax and usage, with examples.