Introduction
In this blog, we will look into the Dart Operators. Dart supports a number of operators, each of which performs a specific operation. We will go through all the types of operators that are supported by the language.
If you are new to Dart and are yet to set up your environment for the language, you can check out this article.
Operators
The operators are special symbols that perform certain operations on the given operands. Every operator performs a certain operation, such as the operator " can be used to verify whether the left operand is less than the second operand. Operators can either be unary or binary. Following are some of the important operators in Dart:
Arithmetic Operators
The following is the list of arithmetic operators that are supported by the Dart language:
- Addition (+): It returns the sum of two operands.
- Subtraction (-): It subtracts the second operand from the first one.
- Multiplication (*): Multiplies the first operand with the second one.
- Divide (/ ): To divide the first operand by the second one.
- Modulus (%): It gives the remainder, which is obtained by dividing the first operand by the second one.
- Division (~/): It gives the integer value obtained by dividing the first operand by the second one.
- Unary Minus (-expr): It reverses the sign of the expressions.
Let's now see an example to understand all these operators:
// Example of Arithmetic operators
void main() {
var first_operand = 20;
var second_operand = 6;
print("The addition(+) of $first_operand with $second_operand = ${first_operand +second_operand}");
print("The subtraction(-) of $first_operand by $second_operand = ${first_operand - second_operand}");
print("The multiplication(*) of $first_operand with $second_operand = ${first_operand * second_operand}");
print("The divide(/) of $first_operand by $second_operand = ${first_operand / second_operand}");
print("The modulus(%) of $first_operand when divided by $second_operand = ${first_operand % second_operand}");
print("The division(~/) of $first_operand by $second_operand = ${first_operand ~/ second_operand}");
print("The unary minus(-expr) of $first_operand = ${-first_operand}");
}
Output:
The addition(+) of 20 with 6 = 26
The subtraction(-) of 20 by 6 = 14
The multiplication(*) of 20 with 6 = 120
The divide(/) of 20 by 6 = 3.3333333333333335
The modulus(%) of 20 when divided by 6 = 2
The division(~/) of 20 by 6 = 3
The unary minus(-expr) of 20 = -20
Bitwise Operators
Following is the list of bitwise operators that are supported by the Dart language:
- Bitwise NOT(~): Flips each bit in the binary representation of a number.
- Bitwise AND(&): It returns one at every bit where both the operands have one.
- Bitwise OR(|): It returns one at every bit where either of the operands has one at that bit.
- Bitwise XOR(^): It returns one at places where both the operands have different bit values at that place.
- Left Shift( <<): In the binary representation of the first operand, it shifts the first operand by the same number of places as the decimal value of the second operand to the left.
- Right Shift( >>): Similar to the left shift, but shifts the first operand to the right.
Let's now see an example to understand all these operators:
// Example of Bitwise operators
void main() {
var first_operand = 20;
var second_operand = 6;
print("The Bitwise(~) of $first_operand = ${~first_operand}");
print("The Bitwise AND(&) of $first_operand with $second_operand = ${first_operand & second_operand}");
print("The Bitwise OR(|) of $first_operand with $second_operand = ${first_operand | second_operand}");
print("The Bitwise XOR(^) of $first_operand with $second_operand = ${first_operand ^ second_operand}");
print("The Left Shift(<<) of $first_operand with $second_operand = ${first_operand << second_operand}");
print("The Right Shift(<<) of $first_operand with $second_operand = ${first_operand >> second_operand}");
}
Output:
The Bitwise(~) of 20 = -21
The Bitwise AND(&) of 20 with 6 = 4
The Bitwise OR(|) of 20 with 6 = 22
The Bitwise XOR(^) of 20 with 6 = 18
The Left Shift(<<) of 20 with 6 = 1280
The Right Shift(<<) of 20 with 6 = 0
Logical Operators
Following is the list of bitwise operators that are supported by the Dart language:
- And(&&): It returns true if both the conditions are true.
- Not(!): It returns true if the statement is false and vice-versa.
- Or(||): It returns true if either of the conditions is true.
Let's now see an example to understand all these operators:
// Example of Logical operators
void main() {
var first_operand = 20;
var second_operand = 6;
if (first_operand == 20 && second_operand == 6) {
print("Both the statements are true");
}
if (first_operand == 20 || second_operand < 6) {
print("At least one of the two statements is true.");
}
if (!(first_operand == 21)) {
print("The first operand is not 21");
}
}
Output:
Both the statements are true
At least one of the two statements is true.
The first operand is not 21
Relational Operators
Following is the list of relational operators that are supported by the Dart language:
- Less than (<): It checks if the first operand is smaller than the second one.
- Greater than (>): It checks if the first operand is greater than the second one.
- Less than (<=): It returns true if the first operand is less than or equal to the second one.
- Greater than(>=): It returns true only if the first operand is greater than or equal to the second one.
- Equal to(==): It returns true if both the operands are equal.
- Not equal to(!=): It returns true if both the operands are not equal.
Let's now see an example to understand all these operators:
// Example of Relational operators
void main() {
var first_operand = 20;
var second_operand = 6;
print("Is $first_operand smaller than $second_operand = ${first_operand < second_operand}");
print("Is $first_operand larger than $second_operand = ${first_operand > second_operand}");
print("Is $first_operand less than or equal to $second_operand = ${first_operand <= second_operand}");
print("Is $first_operand greater than or equal to $second_operand = ${first_operand >= second_operand}");
print("Is $first_operand equal to $second_operand = ${first_operand == second_operand}");
print("Is $first_operand not equal to $second_operand = ${first_operand != second_operand}");
}
Output:
Is 20 smaller than 6 = false
Is 20 larger than 6 = true
Is 20 less than or equal to 6 = false
Is 20 greater than or equal to 6 = true
Is 20 equal to 6 = false
Is 20 not equal to 6 = true
Type Test Operators
These operators are used to check the type of operands. Following is the list of conditional operators that are supported by the Dart language:
- is: Returns true if the operand is of the specified type.
- is not(!): Returns true if the operand is not of the specified type.
Let's now see an example to understand all these operators:
// Example of Type test operators
void main() {
var operand = 20;
print(operand is int);
print(operand is! int);
}
Output:
true
false
Assignment Operators
Following is the list of assignment operators that are supported by the Dart language:
- Equal to (=): It assigns the second operand's value to the first one.
- Assign if null (??=): It assigns the value only if the value of the first operand is null.
- Add and Assign (+=): It assigns the sum of both the operands to the left operand.
- Subtract and Assign(-=): It subtracts the second operand from the first one and assigns the resultant value to the first operand.
- Multiply and Assign(*=): It assigns the multiplication of both the operands to the left operand.
- Divide and Assign(/=): It divides the first operand by the second one and assigns the resultant value to the first one.
Let's now see an example to understand all these operators:
void main() {
var operand = 20;
print("The Value of the operand is: $operand");
var operand2; // null value
operand2 ??= 10;
operand ??= 10; // value of operand will not change as it was not null before
print("operand2: $operand2, operand: $operand");
var first_operand = 50.0;
var second_operand = 17;
first_operand += second_operand;
print("Value after adding and assigning the second operand to the first is: $first_operand");
first_operand -= second_operand;
print("Value after subtracting and assigning the second operand from the first is: $first_operand");
first_operand *= second_operand;
print("Value after multiplying and assigning the second operand from the first is: $first_operand");
first_operand /= second_operand;
print("Value after dividing and assigning the second operand from the first is: $first_operand");
}
Output:
The Value of the operand is: 20
operand2: 10, operand: 20
Value after adding and assigning the second operand to the first is: 67.0
Value after subtracting and assigning the second operand from the first is: 50.0
Value after multiplying and assigning the second operand from the first is: 850.0
Value after dividing and assigning the second operand from the first is: 50.0
Read about Bitwise Operators in C here.
Know more about Unary operator overloading in c++ in detail here.
Frequently Asked Questions
What is the difference between the divide and division operators?
The divide operator gives the absolute division value in decimals, while the division operator gives the integer value of that. For example, 10 divided by 4 will give 2.5, while the division operator will return 2 as the output.
How are unary operators different from binary operators?
Unary operators work only on a single operand, while binary operators need two operands.
Do operators only work on numbers?
No, operators work on other operands also. For example, the addition operator works as a concatenation operator when used on strings.
Conclusion
In this article, we have extensively discussed the Dart Operators. We discussed different types of operators and their specific use in the language. We hope that this blog has helped you enhance your knowledge regarding the Dart Operators, and to learn more, check out our article on Dart Classes. And to learn in-depth about android development, check out our Android Development course on the Coding Ninjas website. Do upvote our blog to help other ninjas grow. Happy Coding!