Assignment Operators
This class of operators contain those operators which are used to assign value to the operands. It goes like this:
Operator Symbol | Operator Name | Operator Description |
---|---|---|
= | 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
void main()
{
int a = 5;
int b = 7;
// Assigning value to variable c
var c = a * b;
print("assignment operator used c = a*b so now c = $c\n");
// Assigning value to variable d
var d;
// Value is assign as it is null
d ??= a + b;
print("Assigning value only if d is null");
print("d??= a+b so d = $d \n");
// Again trying to assign value to d
d ??= a - b;
// Value is not assign as it is not null
print("Assigning value only if d is null");
print("d??= a-b so d = $d");
print("As d was not null value was not updated");
}
Output:
assignment operator used c = a*b so now c = 35
Assigning value only if d is null
d??= a+b so d = 12
Assigning value only if d is null
d??= a-b so d = 12
As d was not null value was not updated
Compound Assignment Operator
Apart from there is another way where we can use a operator that is compound assignment operator where we combine an operator with an assignment operatorso to shorten the steps and make code more effective.
Example:
a+=1;
// The above statement is same as
// the statement mentioned below
a=a+1;
Operators in Dart
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.
Contact Us