Bool, Equality and Relational Operators in Dart
Bool
The bool data type can store only two values: true
or false
. It is used for logical expressions and conditions.
file1.
1234void main() { bool adult = true; bool married = false; }
This variable has the data type bool and stores the value true. This variable has the data type bool and stores the value false.
Equality and Relational Operators
You use these operators in conditional expressions to make decisions in a program. For example, you can check whether a user entered the correct password using the equality operator, or determine if a person has access to a resource based on their age. In such cases, the result of the comparison is a bool value, which helps you control the program flow based on conditions.
main.dart
1234void main() { bool info = 10 > 2; // `true` print(info); }
10 > 2 is a true statement, so we see the result as true
.
main.dart
12345void main(){ String day1 = 'Monday'; String day8 = 'Monday'; print(day1 == day8); // `true` }
The variables day1
and day8
store the same values, so we get true
as a result of the comparison.
There are two equal (==
) signs here because a single equal sign (=
) has a completely different meaning. It is used for assignment and cannot (and does not make sense) be used for boolean expressions.
Data Type Checking
The following operators do not check the value of the variable. They check the data type of the value.
main.dart
123void main() { print(4.2 is int); // `false` }
You get false
because 4.2
is of the double
type.
1. Consider the expression (4 != 5
) and determine whether they evaluate to true
or false
.
2. Consider the expression (6 <= 6 + 3
) and determine whether they evaluate to true
or false
.
3. Consider the expression (4 == 2 + 2
) and determine whether they evaluate to true
or false
.
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Awesome!
Completion rate improved to 4.55
Bool, Equality and Relational Operators in Dart
Swipe um das Menü anzuzeigen
Bool
The bool data type can store only two values: true
or false
. It is used for logical expressions and conditions.
file1.
1234void main() { bool adult = true; bool married = false; }
This variable has the data type bool and stores the value true. This variable has the data type bool and stores the value false.
Equality and Relational Operators
You use these operators in conditional expressions to make decisions in a program. For example, you can check whether a user entered the correct password using the equality operator, or determine if a person has access to a resource based on their age. In such cases, the result of the comparison is a bool value, which helps you control the program flow based on conditions.
main.dart
1234void main() { bool info = 10 > 2; // `true` print(info); }
10 > 2 is a true statement, so we see the result as true
.
main.dart
12345void main(){ String day1 = 'Monday'; String day8 = 'Monday'; print(day1 == day8); // `true` }
The variables day1
and day8
store the same values, so we get true
as a result of the comparison.
There are two equal (==
) signs here because a single equal sign (=
) has a completely different meaning. It is used for assignment and cannot (and does not make sense) be used for boolean expressions.
Data Type Checking
The following operators do not check the value of the variable. They check the data type of the value.
main.dart
123void main() { print(4.2 is int); // `false` }
You get false
because 4.2
is of the double
type.
1. Consider the expression (4 != 5
) and determine whether they evaluate to true
or false
.
2. Consider the expression (6 <= 6 + 3
) and determine whether they evaluate to true
or false
.
3. Consider the expression (4 == 2 + 2
) and determine whether they evaluate to true
or false
.
Danke für Ihr Feedback!