Here's the Part 1 of this tutorial series in case you've missed it. We discussed the primitive data types and the structure of Dart programs there.
Let us now go ahead and have a look at the various operators available in Dart.
Operators are special symbols which are used to perform operations to compare or manipulate data. eg, +
, <
, =
etc
Types of operators in Dart
- Arithmetic Operators
- Equality and Relational Operators
- Type Test Operators
- Assignment Operators
- Logical Operators
- Bitwise and Shift Operators
Arithmetic Operators
These operators are used to do mathematical operations like addition, subtraction etc Here's the list of arithmatic operators supported by Dart:
+
addition-
Subtraction*
multiplication-
reverse the sign of expression/
division~/
integer division%
modulo operation
main() {
var operand1 = 20;
var operand2 = 5;
print(operand1 + operand2);
print(operand1 - operand2);
print(- operand1);
print(operand1 * operand2);
print(operand1 / operand2);
print(operand1 ~/ operand2);
print(operand1 % operand2);
}
Apart from that, like C, Dart also supports prefix and postfix operations:
++var
-> var = var + 1var++
-> var = var + 1--var
-> var = var - 1:var--
-> var = var - 1
Although both var++
and ++var
increment the variable they are applied to, the result returned by var++ is the value of the variable before incrementing, whereas the result returned by ++var is the value of the variable after the increment is applied.
We can see that in the snippet below. The same also applies to --var
and var--
.
main() {
var n1 = 5;
print(++n1); // prints 6
print(n1++); // prints 6
print(n1); // prints 7
print(--n1); // prints 6
print(n1--); // prints 6
print(n1); // prints 5
}
Relational operators
Relational operators compare value of two number type operands and returns a boolean value.
main() {
var operand1 = 50;
var operand2 = 27;
print(operand1 > operand2); // prints true
print(operand1 < operand2); // prints false
print(operand1 >= operand2); // prints true
print(operand1 <= operand2); // prints false
print(operand1 == operand2); // prints false
print(operand1 != operand2); // prints true
}
Type Test operators
Type test operators can be used to check object type at runtime.
main() {
double type1 = 25.0;
int type2 = 58;
String type3 = "Dart Lesson";
bool type4 = true;
print(type1 is int); // prints false
print(type2 is int); // prints true
print(type3 is String); // prints true
print(type4 is double); // prints false
print(type4 is! double); // prints true
}
Assignment operator
Assignment operators are used to assign value to a variable.
Dart also supports compound assignment operator eg +=
, -=
, *=
etc
A += B
is equivalent to A = A + B
. Let us see a sample usage
main() {
var A = 10;
var B = 7;
A += B;
print(A); //prints 17
A *= B;
print(A); //prints 119
A ~/= B;
print(A); //prints 17
}
Logical Operators
Logical operators are used to perform logical AND OR or NOT NOT here is a unary operator.
main() {
var A = true;
var B = false;
var expr = A && B; //false
print(!A); // !true --> false
print(!B); // !false --> true
print(true || expr); // true || expr --> true
print(false || expr); // false || expr --> expr
print(true && expr); // true && expr --> expr
print(false && expr); // false && expr --> false
}
Bitwise and Shift Operators
Bitwise and shift operators perform operations on individual bits of a integer. These are the bitwise operators supported:
&
bitwise AND|
bitwise OR^
bitwise XOR~
bitwise NOT
And these are the supported shift operators
>>
bitwise right shift<<
bitwise left shift
main() {
var A = 10;
var B = 20;
print(~A); // A complement
print(~B); // B complement
print(A & B); // A AND B
print(A | B); // A OR B
print(A ^ B); // A XOR B
print(B << 2); // B Shift Left by 2 bits
print(A >> 2); // A Shift Right by 2 bits
}
That's it for now
Now that we have familiarized ourselves with the different operators Dart provides, let's wrap up our discussion. You can practice your Dart skills on Dartpad which is an online Dart editor.
Next part of this tutorial will start exploring the various collections that we have in Dart so follow my blog to stay updated.
You can also find me on Twitter
Happy Coding. Cheers ๐ป