Dart Operators
Operators are symbols used in programs to perform calculations, comparisons, and logical judgments.
This chapter systematically introduces various operators in Dart, including arithmetic, relational, logical, assignment, conditional operators, and type testing.
* * *
## Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations.
## Example
void main(){
int a =10;
int b =3;
print('a = $a, b = $b');
print('Addition a + b = ${a + b}');// 13
print('Subtraction a - b = ${a - b}');// 7
print('Multiplication a * b = ${a * b}');// 30
print('Division a / b = ${a / b}');// 3.333... (Result is double)
print('Integer division a ~/ b = ${a ~/ b}');// 3 (Truncate, discard decimals)
print('Remainder a % b = ${a % b}');// 1 (Modulo operation)
print('Negation -a = ${-a}');// -10 (Unary minus)
}
a = 10, b = 3Addition a + b = 13Subtraction a - b = 7Multiplication a * b = 30Division a / b = 3.3333333333333335Integer division a ~/ b = 3Remainder a % b = 1Negation -a = -10
> Note: The / operator always returns a double type, even if both operands are int and can be evenly divided. If you only need integer results, use the ~/ integer division operator.
### Increment and Decrement Operators
Like most C-based languages, Dart supports ++ (increment) and -- (decrement).
## Example
void main(){
int count =0;
// Prefix ++οΌincrement first, then use
print('Prefix ++: ${++count}');// 1
// Postfix ++: Use first, then increment
print('Postfix ++: ${count++}');// 1οΌAt this point count is still 1)
print('Now count = $count');// 2
// Decrement works the same way
print('Prefix --: ${--count}');// 1
print('Postfix --: ${count--}');// 1
print('Now count = $count');// 0
}
Prefix ++: 1Postfix ++: 1Now count = 2Prefix --: 1Postfix --: 1Now count = 0
> The prefix and postfix increment/decrement operators behave the same when used alone, but differently in expressions. It is recommended that beginners avoid using them in expressions, writing them on a separate line for clarity.
* * *
## Relational Operators
Relational operators are used to compare two values, and the result is a bool value.
| Operator | Meaning | Example | Result |
| --- | --- | --- | --- |
| == | Equal to | 5 == 5 | true |
| != | Not equal to | 5 != 3 | true |
| > | Greater than | 5 > 3 | true |
| < | Less than | 5 = | Greater than or equal to | 5 >= 5 | true |
| <= | Less than or equal to | 3 = passLine}');
print('Perfect score?? ${tutorialScore == 100}');
// Strings can also be compared
String a ='apple';
String b ='banana';
print("'$a' == '$b'? ${a == b}");
// == Checks if values are equal
String c ='TUTORIAL';
String d ='TUTORIAL';
print("'$c' == '$d'? ${c == d}");// trueοΌSame content)
}
Score: 95, Passing score: 60Did you pass?? truePerfect score?? false'apple' == 'banana'? false'TUTORIAL' == 'TUTORIAL'? true
> In Dart, == compares whether two objects have equal values, not reference addresses. This is different from Javaβin Dart you don't need to use the equals() method to compare strings.
* * *
## Logical Operators
Logical operators are used to combine multiple bool expressions, typically used for conditional judgments.
| Operator | Meaning | Description |
| --- | --- | --- |
| && | Logical AND | Returns true only if
```
YouTip