Operators
Anonymous contributor
Anonymous contributor1 total contribution
Anonymous contributor
Published Feb 9, 2024
Contribute to Docs
Operators in Dart are special symbols or phrases used to perform operations on variables and values. Dart, like many other programming languages, includes a variety of operators to manipulate data in different ways. These operators are categorized into several types based on their functionality: arithmetic, equality and relational, type test, logical, bitwise, and assignment operators.
Syntax
Operators in Dart follow typical programming syntax and are used in conjunction with operands. The general syntax can be described as:
operand1 operator operand2
Types of Operators
Category | Operator | Description |
---|---|---|
Arithmetic Operators | + |
Addition |
- |
Subtraction | |
* |
Multiplication | |
/ |
Division | |
% |
Modulus | |
~/ |
Truncating Division | |
Equality and Relational Operators | == |
Equal |
!= |
Not Equal | |
> |
Greater Than | |
< |
Less Than | |
>= |
Greater Than or Equal To | |
<= |
Less Than or Equal To | |
Type Test Operators | is |
True if the object has the specified type |
is! |
True if the object does not have the specified type | |
Logical Operators | && |
Logical AND |
|| |
Logical OR | |
! |
Logical NOT | |
Bitwise Operators | & |
Bitwise AND |
| |
Bitwise OR | |
^ |
Bitwise XOR | |
~ |
Bitwise NOT | |
<< |
Left shift | |
>> |
Right shift | |
Assignment Operators | = |
Simple Assignment |
+= |
Add and Assign | |
-= |
Subtract and Assign | |
*= |
Multiply and Assign | |
/= |
Divide and Assign | |
%= |
Modulus and Assign | |
~/= |
Truncating Divide and Assign |
Example
Here is a simple Dart program that demonstrates the use of various operators:
void main() {// Arithmetic Operatorsint a = 10;int b = 5;print('a + b = ${a + b}');print('a - b = ${a - b}');print('a * b = ${a * b}');print('a / b = ${a / b}');print('a % b = ${a % b}');print('a ~/ b = ${a ~/ b}');// Equality and Relational Operatorsprint('a == b: ${a == b}');print('a != b: ${a != b}');print('a > b: ${a > b}');print('a < b: ${a < b}');print('a >= b: ${a >= b}');print('a <= b: ${a <= b}');// Logical Operatorsbool x = true;bool y = false;print('x && y: ${x && y}');print('x || y: ${x || y}');print('!x: ${!x}');// Bitwise Operatorsint c = 3;int d = 5;print('c & d: ${c & d}');print('c | d: ${c | d}');// Assignment Operatorsint e = 5;e += 2;print('e: $e');}
The above code will give the following output:
a + b = 15a - b = 5a * b = 50a / b = 2a % b = 0a ~/ b = 2a == b: falsea != b: truea > b: truea < b: falsea >= b: truea <= b: falsex && y: falsex || y: true!x: falsec & d: 1c | d: 7e: 7
All contributors
- Anonymous contributorAnonymous contributor1 total contribution
- Anonymous contributor
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.