Operators are the special symbols which stands for specific type of action or operation between operands. For Ex, * is an operator symbol used to perform specific operation which is multiplication between two numbers.
What is operand ?
Operands are the elements which are manipulated by operators.
For ex :
var multiplication = X * Y ;
Here, X and Y are operands which are manipulated by * operator which represents multiplication operation.
There are so many types of operators available in dart as below.
- Arithmetic operators
- Equality or relational operators
- Assignment operators
- Logical operators
- Bitwise operators
- Miscellaneous operators
We will try to cover all the important and useful operators in dart. Let’s start with arithmetic operators.
Arithmetic operators
Arithmetic operators are used to perform basic mathematical operations like, addition, subtraction, multiplication etc.
+ | Addition between operands |
– | Subtraction between operands |
* | Multiplication between operands |
/ | Division between operands |
~/ | Division between operands, which gives result in integer value |
% | Provides the remainder of the division operation |
Let’s take an example to briefly understand arithmetic operators.
For ex :
void main(){
int x = 15, y = 8;
var addition = x + y ;
print("Addition : ${addition}");
var subtraction = x - y ;
print("Subtraction : ${subtraction}");
var multiplication = x * y ;
print("Multiplication : ${multiplication}");
var division = x / y ;
var remainder = x % y ;
print("Division : ${division} And Remainder : ${remainder}");
var roundOffDivision = x ~/ y ;
print("RoundOffDivision : ${roundOffDivision}");
}
Output :
Addition : 23
Subtraction : 7
Multiplication : 120
Division : 1.875 And Remainder : 7
RoundOffDivision : 1
Equality or relational operators
Equality operators are used to generate conditional expressions. Basically, equality opeartors compare two different operands based on relations between them. (greater than, less than , equal to and so on)
== | Checks if the compared operands(elements) have equal value |
!= | Checks if the compared operands(elements) have not equal value |
> | Checks If the first operand has greater value than second operand |
< | Checks If the second operand has lesser value than first operand |
>= | Checks If the first operand is greater value than or equal to second operand value |
<= | Checks If the second operand has lesser or equal to than first operand value |
Let’s take an example to briefly understand equality operators.
For ex :
void main(){
int x = 15, y = 8, z=8;
if(x > y){
print (" $x is greater than $y");
}
if(y < x){
print (" $y is less than $x");
}
if(y == z){
print (" $y is equal to $z");
}
if(x != y){
print (" $x is not eqaul to $y");
}
if(x >= y){
print (" $x is greater than or equal to $y");
}
if(y <= x){
print ("$y is less than or equal to $x");
}
}
Output :
15 is greater than 8
8 is less than 15
8 is equal to 8
15 is not equal to 8
15 is greater than or equal to 8
8 is less than or equal to 15
Assignment operators
Assignment operators are used to assign the value to any variable.
Syntax :
// here variable and operand must be of the same data type.
variableName assignmentOperator operand
In above Example, assignmentOperator can be any Assignment operator like, +=, -=, *=, /=, %= etc.
+= | Assigns right side addition(+) operation result to the left side variable. |
-= | Assigns right side subtraction(-) operation result to the left side variable. |
*= | Assigns right side multiplication(*) operation result to the left side variable. |
/= | Assigns right side division(/) operation result to the left side variable. |
%= | Assigns right side division operation remainder(%) result to the left side variable. |
For ex :
a+=5;
/*is equal to a=a+5;Here, a+5 is addition operation between two operands (a and 5) whose result is assigned to the left side variable a;*/
Let’s take an example to briefly understand the Assignment operators.
void main(){
double x = 15, y = 10;
print("Addition : ${x+=y}");
print("Subtraction : ${x-=y}");
print("Multiplication : ${x*=y}");
print("Division : ${x/=y}");
print("DivisionRemainder : ${x%=y}");
}
Output :
Addition : 25
Subtraction : 15
Multiplication : 150
Division : 15
DivisionRemainder : 5
Logical operators
These operators are used to combine multiple boolean expressions to perform different logical operations.
&& | Logical AND (both boolean expression’s result must be true.) |
|| | Logical OR (any one boolean expression’s result must be true.) |
For ex :
void main(){
int a =15, b=10;
if(a > 0 && a > b){
print("$a is my favourite number");
}
if(a < b || a >b){
print("$b is my favourite number");
}
}
Output :
15 is my favourite number
10 is my favourite number
For Logical AND operator (&&) both expressions at each side of the operator must be evaluated true. While for Logical OR operator(||) only one expression result should be true.
There is an interesting thing about Logical AND operator that it doesn’t check the second expression if the first expression result is false. As Logical AND operator requires both expression result to be true.
Similarly, Logical OR operator doesn’t check the second expression if the first expression result is true. As Logical OR operator requires only one expression result to be true. Due to this short-trick of easily evaluating logical expressions logical operators are also known as short-hand operators.
Bitwise Operators
Bitwise operators are used to perform bit operations on individual integer numbers only.
Their working is similar to Logical Gates (AND, OR, XOR, etc) available in digital electronics to perform different bit operations on numbers.
Real life use-cases of bitwise operators is communication over usb ports/sockets or in data comparison or encryption.
These operators work on binary values i.e, 0 and 1 to manipulate data bit by bit.
& – Bitwise AND Operators
The & operator compares its both operands using Logical AND operation similar to digital electronics.
If both bits are 1 then It gives resultant bit 1 else resultant bit will be 0.
| – Bitwise OR Operators
The | operator compares its both operands using Logical OR operation similar to digital electronics.
If any of the bit is 1 then It gives resultant bit 1 else resultant bit will be 0.
^ – Bitwise XOR Operators
The ^ operator compares its both operands using Logical XOR operation similar to digital electronics.
If both bits are different resultant bit is 1 else resultant bit will be 0.
Let’s take an example to briefly understand the Bitwise operators.
For ex :
void main(){
int a =15; // binary value of a is 1111
int b=10; // binary value of a is 1010
var AND = a & b; // 1111 & 1010 = 1010 which is binary value of 10
var OR = a | b; // 1111 | 1010 = 1111 which is binary value of 15
var XOR = a ^ b; // 1111 ^ 1010 = 0101 which is binary value of 5
print("Value of Bitwise AND Operation is $AND");
print("Value of Bitwise OR Operation is $OR");
print("Value of Bitwise XOR Operation is $XOR");
}
Output :
Value of Bitwise AND Operation is 10
Value of Bitwise OR Operation is 15
Value of Bitwise XOR Operation is 5
Miscellaneous Operators
In this part, we are going to discuss some other useful operators which have significance usage for different cases.
Let’s start with Increment/Decrement Operators,
Increment / Decrement Operators
Dart has special type of operators known as Increment/Decrement operators.
Increment and Decrement operators are used to increment and decrement the value of the particular value by 1 respectively.
Increment operators are denoted using ++ while decrement operators are denoted using — symbol.
increment/decrement operator symbol prefixed before the variable is known as pre increment/decrement operators. While, operator symbol postfixed after the variable is known as post increment/decrement variables.
pre increment operator (++a)
The value of the variable (a) is incremented by 1 first. Later incremented value is returned or assigned to the expression (++a).
pre decrement (–a)
The value of the variable (a) is decremented by 1 first. Later decremented value is returned or assigned to the expression (–a).
post increment operator (a++)
The value of the variable a is returned or assigned to the expression (a++) first. Later the value of the variable (a) is incremented by 1.
post decrement operator (a–)
The value of the variable a is returned or assigned to the expression (a–) first. Later the value of the variable (a) is decremented by 1.
Let’s take an example to briefly understand the increment/decrement operators.
For ex :
void main(){
int a =10, b=20, c=30, d=40;
var preAdd=--a;
var preSub=--b;
print("value of the a is $a and value of pre increment expression is ${preAdd}");
print("value of the b is $b and value of pre decrement expression is ${preSub}");
var postAdd=c++;
var postSub=d--;
print("value of the a is $c and value of post increment expression is ${postAdd}");
print("value of the b is $d and value of post decrement expression is ${postSub}");
}
Output :
value of the a is 9 and value of pre increment expression is 9 value of the b is 19 and value of pre decrement expression is 19 value of the a is 31 and value of post increment expression is 30 value of the b is 39 and value of post decrement expression is 40
Ternary Operator
Dart has one special operator to simplify the if else condition which is known as conditional operator or ternary operator.
Syntax :
conditional_expression ? expression1 : expression2
In Ternary operator, it first checks the conditional expression. If the condition is true then expression1 is evaluated otherwise expression2 is evaluated.
For ex :
void main(){
int a=10, b=20;
b > a ? print("Greater no = $b") : print("Greater no = $a") ;
}
Output :
Greater no = 20
is Operator
Dart has is operator to check whether an object is an instance of the specified type or not.
is operator is generally used while type-casting objects to avoid unnecessary exceptions.
Syntax :
instance is type
For ex :
class Data{
}
class Test{
}
void main(){
Test test=new Test();
// test is an instance of Data class. While Test is the type of the object reference.
if(test is Test){
print("test is the instance of Test class");
}
if(test is Data){
print("test is the instance of Data class");
}
}
Output :
test is the instance of Test class
Member Access Operator (.)
To access any property or methods of a class, Member Access operator is used.
Member access operator is denoted with “.” symbol.
For ex :
class Test{
int a=5; // a is an instance variable
}
void main() {
Test test=new Test(); // new keyword is used to create instance of a class.
print(test.a); // “.” is a Member Access operator
}
Output :
5
So, these are different operators available in dart. Which makes our operation very handy and smoother by providing their built-in functionalities
We have tried to cover all the important dart operators. Although you can visit dart’s official site to cover any missing data related to operators. Also feel free to suggest any changes to make our tutorials more interactive and interesting.
Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.