In this chapter, we will learn about different Dart Operators that are available in dart.
Dart Operators
Operators are symbols that are used to perform certain operations. For example,
5 - 3;
Here – is the operator and 5 and 3 are the operands.
– operator is used to subtract the value from 5 and 3, which results in 2.
Dart Operator Types
Dart supports all the operators that are available in modern programming languages.
- Arithmetic Operators
- Comparison Operators
- Assignment Operators
- Type test Operators
- Bitwise Operators
- Logical Operators
- Conditional Operators
Dart Arithmetic Operators
Arithmetic operators help in performing arithmetic operations. For example,
void main() { int num = 5 - 3; print(num); // 2 }
Here, – operator is used to subtract value 3 from value 5, which results in 2.
Operator | Name | Description |
+ | Addition | Adds two operands |
– | Subtraction | Subtracts two operands |
-expr | Unary Minus | Reverse the sign of the expression |
* | Multiply | Multiply two operands |
/ | Division | Divide two operands |
~/ | Division | Divide two operands and give integer output |
% | Modulus | Give reminder of two operands |
++ | Increment | Increments the value by 1 |
– – | Decrement | Decrements the value by 1 |
Arithmetic Operators Examples
void main() { var num1 = 3 + 4; // 7 var num2 = 4 - 3; // 1 var num3 = 4 * 3; // 12 var num4 = 4 / 3; // 1.3333333333333333 var num5 = 4 ~/ 3; // 1 var num6 = 4 % 3; // 1 }
Dart Comparison Operators
Comparison operators help in comparing two operands and give boolean results true or false. For example,
void main() { var num = 5 > 3; print(num); // true }
Operator | Name | Description |
== | Equal | True if both operands are equal |
!= | Not Equal | True if both operands are not equal |
> | Greater than | True if left operand is greater than right operand |
< | Less than | True if left operand is less than right operand |
>= | Greater than or equal | True if left operand is greater than or equal to right operand |
<= | Less than or equal | True if left operand is less than or equal to right operand |
Comparison Operators Examples
void main() { var num1 = 3 < 4; // true var num2 = 4 > 3; // false var num3 = 3 >= 3; // true var num4 = 3 <= 3; // true var num5 = 3 == 3; // true var num6 = 4 != 3; // true }
Dart Assignment Operators
Assignment operator help is assigning a value to a variable. For example,
var num = ‘hello’;
Operator | Name | Description |
= | Assignment | Assign values to variable |
-= | Subtraction Assignment | Assign value after subtraction |
+= | Addition Assignment | Assign value after addition |
/= | Division Assignment | Assign value after division |
%= | Modulus Assignment | Assign value after modulus |
*= | Multiply Assignment | Assign value after multiplication |
~/= | Integer Division Assignment | Assign value after integer division |
Assignment Operators Examples
void main() { var num1 = 5; // 5 var num1 -= 4; // num1 = num1 - 4 var num3 +=4; // num1 = num1 + 4 var num4 /= 5; // num1 = num1 / 5 var num5 *= 5; // num1 = num * 5 }
Type Test Operators
Type test operators help in comparing the operands. For example,
void main() { String a = 'hello'; int b = 3; print(a is String); // true print(b is !int); // false }
Operator | Name | Description |
is | is | True if specified data type |
is! | is not | True if not specified data type |
Dart Bitwise Operators
Bitwise operators help in performing bitwise operations. For example,
void main() { int a = 3; int b = 2; // Bitwise AND var c = a & b; print(c); // 2 }
Operator | Name | Description |
& | AND | AND operation on two operands |
| | OR | OR operation on two operands |
^ | XOR | XOR operation on two operands |
~expr | Unary bitwise complement | 0s become 1s; 1s become 0s |
<< | Shift Left | Shifts a in binary representation to b bits to left and inserting 0 from right. |
>> | Shift Right | Shifts a in binary representation to b bits to left and inserting 0 from left. |
Dart Logical Operators
Logical operators help in performing logical operations and return a boolean value. For example,
void main() { int a = 7; int b = 2; // And Operator bool c = a > 10 && b < 10; print(c); // false }
Operator | Name | Description |
&& | AND | True if both the operands are true |
|| | OR | True if either of the operands is true |
! | NOT | True if the operand is false |
That’s it for the operators. In the next chapter, we will learn about the Dart Comments and how we can use them in our program.