The operators are special symbols that are used to carry out certain operations on the operands. The Dart has numerous built-in operators which can be used to carry out different functions, for example, ‘+’ is used to add two operands. Operators are meant to carry operations on one or two operands.
Different types of operators in Dart:
The following are the various types of operators in Dart:
- Arithmetic Operators
- Relational Operators
- Type Test Operators
- Bitwise Operators
- Assignment Operators
- Logical Operators
- Conditional Operator
- Cascade Notation Operator
1. Arithmetic Operators:
This class of operators contain those operators which are used to perform arithmetic operation on the operands. They are binary operators i.e they act on two operands. They go like this:
+ |
Addition |
Use to add two operands |
– |
Subtraction |
Use to subtract two operands |
-expr |
Unary Minus |
It is Use to reverse the sign of the expression |
* |
Multiply |
Use to multiply two operands |
/ |
Division |
Use to divide two operands |
~/ |
Division |
Use two divide two operands but give output in integer |
% |
Modulus |
Use to give remainder of two operands |
Example: Using Arithmetic Operators in the program
Dart
void main()
{
int a = 2;
int b = 3;
var c = a + b;
print( "Sum of a and b is $c" );
var d = a - b;
print( "The difference between a and b is $d" );
var e = -d;
print( "The negation of difference between a and b is $e" );
var f = a * b;
print( "The product of a and b is $f" );
var g = b / a;
print( "The quotient of a and b is $g" );
var h = b ~ / a;
print( "The quotient of a and b is $h" );
var i = b % a;
print( "The remainder of a and b is $i" );
}
|
Output:
Sum of a and b is 5
The difference between a and b is -1
The negation of difference between a and b is 1
Product of a and b is 6
The quotient of a and b is 1.5
The quotient of a and b is 1
The remainder of a and b is 1
2. Relational Operators:
This class of operators contain those operators which are used to perform relational operation on the operands. It goes like this:
> |
Greater than |
Check which operand is bigger and give result as boolean expression. |
< |
Less than |
Check which operand is smaller and give result as boolean expression. |
>= |
Greater than or equal to |
Check which operand is greater or equal to each other and give result as boolean expression. |
<= |
less than equal to |
Check which operand is less than or equal to each other and give result as boolean expression. |
== |
Equal to |
Check whether the operand are equal to each other or not and give result as boolean expression. |
!= |
Not Equal to |
Check whether the operand are not equal to each other or not and give result as boolean expression. |
Example: Using Relational Operators in the program
Dart
void main()
{
int a = 2;
int b = 3;
var c = a > b;
print( "a is greater than b is $c" );
var d = a < b;
print( "a is smaller than b is $d" );
var e = a >= b;
print( "a is greater than b is $e" );
var f = a <= b;
print( "a is smaller than b is $f" );
var g = b == a;
print( "a and b are equal is $g" );
var h = b != a;
print( "a and b are not equal is $h" );
}
|
Output:
a is greater than b is false
a is smaller than b is true
a is greater than b is false
a is smaller than b is true
a and b are equal is false
a and b are not equal is true
3. Type Test Operators:
This class of operators contain those operators which are used to perform comparison on the operands. It goes like this:
is |
is |
Gives boolean value true as output if the object has specific type |
is! |
is not |
Gives boolean value false as output if the object has specific type |
Example: Using Type Test Operators in the program
Dart
void main()
{
String a = 'GFG' ;
double b = 3.3;
print(a is String);
print(b is ! int );
}
|
Output:
true
true
4. Bitwise Operators:
This class of operators contain those operators which are used to perform bitwise operation on the operands. It goes like this:
& |
Bitwise AND |
Performs bitwise and operation on two operands. |
| |
Bitwise OR |
Performs bitwise or operation on two operands. |
^ |
Bitwise XOR |
Performs bitwise XOR operation on two operands. |
~ |
Bitwise NOT |
Performs bitwise NOT operation on two operands. |
<< |
Left Shift |
Shifts a in binary representation to b bits to left and inserting 0 from right. |
>> |
Right Shift |
Shifts a in binary representation to b bits to left and inserting 0 from left. |
Example: Using Bitwise Operators in the program
Dart
void main()
{
int a = 5;
int b = 7;
var c = a & b;
print(c);
var d = a | b;
print(d);
var e = a ^ b;
print(e);
var f = ~a;
print(f);
var g = a << b;
print(g);
var h = a >> b;
print(h);
}
|
Output:
5
7
2
4294967290
640
0
5. Assignment Operators:
This class of operators contain those operators which are used to assign value to the operands. It goes like this:
= |
Equal to |
Use to assign values to the expression or variable |
??= |
Assignment operator |
Assign the value only if it is null. |
Example: Using Assignment Operators in the program
Dart
void main()
{
int a = 5;
int b = 7;
var c = a * b;
print(c);
var d;
d ? ? = a + b;
print(d);
d ? ? = a - b;
print(d);
}
|
Output:
35
12
12
6. Logical Operators:
This class of operators contain those operators which are used to logically combine two or more conditions of the operands. It goes like this:
&& |
And Operator |
Use to add two conditions and if both are true than it will return true. |
|| |
Or Operator |
Use to add two conditions and if even one of them is true than it will return true. |
! |
Not Operator |
It is use to reverse the result. |
Example 1: Using Logical Operators in the program
Dart
void main()
{
int a = 5;
int b = 7;
bool c = a > 10 && b < 10;
print(c);
bool d = a > 10 || b < 10;
print(d);
bool e = !(a > 10);
print(e);
}
|
Output:
false
true
true
Example 2: Logical operator can only be application to boolean expression and in dart, non-zero numbers are not considered as true and zero as false:
Dart
void main()
{
int a = 5;
int b = 7;
print(a && b);
print(a || b);
print(!a);
}
|
Output: Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
7. Conditional Operators:
This class of operators contain those operators which are used to perform comparison on the operands. It goes like this:
condition ? expersion1 : expersion2 |
Conditional Operator |
It is a simple version of if-else statement. If the condition is true than expersion1 is executed else expersion2 is executed. |
expersion1 ?? expersion2 |
Conditional Operator |
If expersion1 is non-null returns its value else returns expression2 value. |
Example: Using Conditional Operators in the program
Dart
void main()
{
int a = 5;
int b = 7;
var c = (a < 10) ? "Statement is Correct, Geek" : "Statement is Wrong, Geek" ;
print(c);
int ? n;
var d = n ?? "n has Null value" ;
print(d);
n = 10;
d = n;
print(d);
}
|
Output:
Statement is Correct, Geek
n has Null value
10
Also In The Above Code,You May Notice That The Variable ‘n’ is Declared As “int? n”.By declaring n as int?, you are indicating that the variable n can hold an integer value or a null value. The ? denotes that the variable is nullable, meaning it can be assigned a null value in addition to integer values.
8. Cascade Notation Operators:
This class of operators allows you to perform a sequence of operation on the same element. It allows you to perform multiple methods on the same object. It goes like this:
.. |
cascading Method |
It is used to perform multiple methods on the same object. |
Example: Using Cascade Notation Operators in the program
Dart
class GFG {
var a;
var b;
void set(x, y)
{
this .a = x;
this .b = y;
}
void add()
{
var z = this .a + this .b;
print(z);
}
}
void main()
{
GFG geek1 = new GFG();
GFG geek2 = new GFG();
geek1.set(1, 2);
geek1.add();
geek2..set(3, 4)
..add();
}
|
Output:
3
7
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
11 Oct, 2023
Like Article
Save Article