A Comprehensive Guide to Operators
Let's explore most common operators in Dart.
Arithmetic Operators in Dart
Dart has all the basic arithmetic operators that we need to perform calculations or evaluating numerical expressions.
Addition/Sum Operator
- This operator is used to carry out the sum of two operands.
- We use
+operator to perform the addition.
void main() {
int addition = 10 + 3;
print("Sum = $addition");
}
Sum = 13
Subtraction/Difference Operator
- This operator is used to carry out the difference of two operands.
- We use
-operator to perform the subtraction.
void main() {
int subtraction = 10 - 3;
print("Difference = $subtraction");
}
Difference = 7
Multiplication/Product Operator
- This operator is used to carry out the product of two operands.
- We use
*operator to perform the multiplication.
void main() {
int multiplication = 10 * 3;
print("Product = $multiplication");
}
Product = 30
Division/Quotient Operator
- This operator is used to carry out the division of two operands.
- We use
/operator to perform the quotient operation.
void main() {
int division = 10 / 3;
print("Quotient = $division");
}
Quotient = 3.3333333333333335
Integer Division Operator
- This operator is used to carry out the integer division of two operands.
- This truncates/trims off the decimal part from the actual division.
- We use
~/operator to perform the integer division.
void main() {
int intDivision = 10 ~/ 3;
print("Integer Division = $intDivision");
}
Integer Division = 3
Remainder/Modulo Operator
- This operator is used to carry out the remainder between two operands, when one operand is divided by the other.
- We use
%operator to perform the modulo operation.
void main() {
int modulo = 10 % 3;
print("Remainder = $modulo");
}
Remainder = 1
Increment and Decrement Operators
-
When we want to increment or decrement the value of a variable by 1, then we can use the increment or decrement operators.
-
Increment operator is written as
++, where as the decrement operator is written as--.
Using the Increment/Decrement operators directly without assignment
In the following code, ++ increments the number variable by 1 and -- decrements the same variable by 1.
void main() {
int number = 10;
number++; // this increments the number variable - 10 + 1 = 11
number--; // this decrements the number variable - 11 - 1 = 10
print(number); // number variable returns back to the original state in line 2
}
10
Using the Increment/Decrement operators with assignment
The output of each print statement is written in the following code block, and is self-explanatory!
void main() {
int secondNumber = 100;
int thirdNumber = secondNumber++; // increment operator is suffixed here - post-increment operator - assign then increment
print(secondNumber); // 101
print(thirdNumber); // 100
int fourthNumber = ++secondNumber; // increment operator is prefixed here - pre-increment operator - increment then assign
print(secondNumber); // 102
print(fourthNumber); // 102
int fifthNumber = secondNumber--; // decrement operator is suffixed here - post-decrement operator - assign then decrement
print(secondNumber); // 101
print(fifthNumber); // 102
int sixthNumber = --secondNumber; // decrement operator is prefixed here - pre-decrement operator - decrement then assign
print(secondNumber); // 100
print(sixthNumber); // 100
}
Compound Assignment Operators
The usage covering the code of compound assignment operators is given below:
void main() {
int number = 100;
number += 10; // same as number = number + 10
print(number);
number -= 20; // same as number = number - 20
print(number);
number *= 3; // same as number = number * 3
print(number);
number ~/= 60; // same as number = number ~/ 60
print(number);
}
110 90 270 4
These are just shorthand operators.