Summary: in this tutorial, you’ll learn how to use the Dart bool type that holds boolean values including true
and false
.
Introduction to the Dart bool type
Dart uses the bool
type to represent Boolean values. The bool
type has two constant values true
and false
.
The following example declares a variable with the bool
type and initialize its value to true
:
void main() {
bool isActive = true;
print(isActive);
}
Code language: Dart (dart)
In practice, you often use the boolean values in the control statements such as if else
, while
, do while
, and for
.
Logical operators
Dart provides you with logical operators for working with boolean values as illustrated in the following table:
Operator | Name | Meaning |
---|---|---|
&& | Logical AND | Accept two boolean values and return true if both of them are true or false otherwise |
|| | Logical OR | Accept two boolean values and return false if both values are false or true otherwise |
! | Logical NOT | Accept a boolean value and negate it. |
Logical AND operator (&&)
The following table shows the result of the logical AND operator:
x | y | x && y |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
For example, the following program illustrates how to use the logical AND operator:
void main() {
bool isActive = true;
bool isPasswordCorrect = true;
bool isAuthenticated = isPasswordCorrect && isActive;
print(isAuthenticated);
}
Code language: Dart (dart)
Output:
true
Code language: Dart (dart)
In this example, both isActive
and isPasswordCorrect
are true. Therefore, the result of the logical AND of these variables is true
.
Logical OR operator (||)
The following table illustrates the result of the logical OR operator:
x | y | x || y |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
The table shows that x || y
returns false
only if both values are false
. Otherwise, it returns true
.
For example, the following program shows how to use the logical OR operator:
void main() {
bool isRequired = false;
bool isValid = false;
bool result = isRequired || isValid;
print(result);
}
Code language: Dart (dart)
In this example, both isRequired
and isValid
are false
. Therefore, the result of the expression isRequired || isValid
is false
.
Short-circuit evaluation
Both logical AND and OR operators are short-circuited.
If the first value is false, the logical AND operator will be false regardless of the value of the second value. Therefore, the logical AND operator will not evaluate the second value if the first one is false
. For example:
void main() {
bool isRequired = false;
bool isValid = true;
bool result = isRequired && isValid;
print(result);
}
Code language: Dart (dart)
In this example, the logical AND operator doesn’t evaluate the isValid
variable because the result is always false
due to the value of the isRequired
.
In VS Code, you’ll see the following warning message for the isValid
variable in the expression:
Dead code.
Try removing the code, or fixing the code before it so that it can be reached
Code language: Dart (dart)
It means that the isValid
will never be reached.
Similarly, if the first value is true
, the logical OR operator will return true
whether the value of the second value is true
or false
. Therefore, the logical OR operator will not evaluate the second value if the first one is true
.
The following program illustrates how the logical OR operator is short-circuited:
void main() {
bool isRequired = true;
bool isValid = false;
bool result = isRequired || isValid;
print(result);
}
Code language: Dart (dart)
In this example, the isRequired
is true. Therefore, the result
is always true
. The logical OR operator will not evaluate the isValid
in the expression isRequired || isValid
.
Logical NOT operator (!)
The logical NOT operator negates the value of a boolean value:
x | !x |
---|---|
true | false |
false | true |
The following program uses the logical OR operator to negate a value of the variable isRequired
:
void main() {
bool isRequired = true;
print(!isRequired);
}
Code language: Dart (dart)
Output:
false
Code language: Dart (dart)
Summary
- The
bool
type to represent boolean values includingtrue
andfalse
. - The logical AND operator (
&&
) returnstrue
only if both values aretrue
. - The logical OR operator (
||
) returnsfalse
only if both values arefalse
. - Both logical AND and OR operators are short-circuited.
- The logical NOT operator (
!
) negates a boolean value.