Dart Operators
In Dart, operators are special symbols that perform operations on variables and values. Dart supports a wide range of operators, which can be categorized into several types based on their functionality. Here’s an overview of the different types of operators available in Dart:
1. Arithmetic Operators
These operators perform basic mathematical operations.
Operator | Description | Example |
---|---|---|
+ | Addition | 5 + 3 (results in 8 ) |
- | Subtraction | 5 - 3 (results in 2 ) |
* | Multiplication | 5 * 3 (results in 15 ) |
/ | Division | 5 / 2 (results in 2.5 ) |
~/ | Integer Division | 5 ~/ 2 (results in 2 ) |
% | Modulus (Remainder) | 5 % 2 (results in 1 ) |
2. Relational Operators
These operators compare two values and return a boolean result (true
or false
).
Operator | Description | Example |
---|---|---|
== | Equal to | 5 == 3 (results in false ) |
!= | Not equal to | 5 != 3 (results in true ) |
> | Greater than | 5 > 3 (results in true ) |
< | Less than | 5 < 3 (results in false ) |
>= | Greater than or equal to | 5 >= 5 (results in true ) |
<= | Less than or equal to | 5 <= 3 (results in false ) |
3. Logical Operators
These operators are used to perform logical operations, returning a boolean value.
Operator | Description | Example |
---|---|---|
&& | Logical AND | (5 > 3) && (3 < 4) (results in true ) |
` | ` | |
! | Logical NOT | !(5 > 3) (results in false ) |
4. Assignment Operators
These operators are used to assign values to variables.
Operator | Description | Example |
---|---|---|
= | Assigns a value | int a = 5; |
+= | Add and assign | a += 3; (equivalent to a = a + 3 ) |
-= | Subtract and assign | a -= 2; (equivalent to a = a - 2 ) |
*= | Multiply and assign | a *= 2; (equivalent to a = a * 2 ) |
/= | Divide and assign | a /= 2; (equivalent to a = a / 2 ) |
~/= | Integer divide and assign | a ~/= 2; (equivalent to a = a ~/ 2 ) |
%= | Modulus and assign | a %= 2; (equivalent to a = a % 2 ) |
5. Conditional Operator
The conditional operator (?:
) is a shorthand for an if-else
statement, often referred to as the ternary operator.
Operator | Description | Example |
---|---|---|
?: | Ternary (Conditional) | var result = (a > b) ? a : b; (assigns the greater of a or b to result ) |
6. Type Test Operators
These operators are used to check the type of a variable.
Operator | Description | Example |
---|---|---|
is | Checks if an object is of a specific type | if (x is String) (checks if x is a String ) |
is! | Checks if an object is not of a specific type | if (x is! String) (checks if x is not a String ) |
7. Bitwise Operators
These operators perform operations on the binary representations of integers.
Operator | Description | Example |
---|---|---|
& | Bitwise AND | 5 & 3 (results in 1 ) |
` | ` | Bitwise OR |
^ | Bitwise XOR | 5 ^ 3 (results in 6 ) |
~ | Bitwise NOT | ~5 (results in -6 ) |
<< | Left shift | 5 << 1 (results in 10 ) |
>> | Right shift | 5 >> 1 (results in 2 ) |
8. Cascade Operator
The cascade operator (..
) allows you to make a series of calls on the same object without having to repeat the object reference.
Example:
Summary
Dart provides a comprehensive set of operators that allow developers to perform various operations on data. Understanding how to use these operators effectively is essential for writing clear and concise Dart code. Whether you’re performing arithmetic calculations, making comparisons, or manipulating data structures, operators play a crucial role in controlling the flow and logic of your Dart programs.