Operators in Dart
Operators In Dart
Operators are used to performing mathematical and logical operations on the variables. Each operation in dart uses a symbol called operator to denote the type of operation it performs.
Types Of Operator
- Arithmetic Operator
- Assignment Operator
- Comparison Operator
- Logical Operator
Arithmetic Operator
It is used to perform common mathematical operations. They are:
+
: For Addition-
: For Subtraction*
: For Multiplication/
: For Division%
: For Remainder After Division++
: Increase Value By 1. For E.g a++;--
: Decrease Value By 1. For E.g a- -;
void main() {
int num1=10;
int num2=5;
int sum=num1+num2; // addition
int diff=num1-num2; // subtraction
int mul=num1*num2; // multiplication
double div=num1/num2; // division
int mod=num1%num2; // show remainder
//Printing info
print(sum);
print(diff);
print(mul);
print(div);
print(mod);
}
Assignment Operators
It is used to assign some values to variables. Here, we are assigning 24 to the age variable.
int age = 24;
+=
adds a value to a variable.-=
reduces a value to a variable.*=
multiply value to a variable./=
divided value by a variable.
void main() {
double age = 24;
age+= 1; // Here age+=1 means age = age + 1.
print("After Addition Age is $age");
age-= 1; //Here age-=1 means age = age - 1.
print("After Aubtraction Age is $age");
age*= 2; //Here age*=2 means age = age * 2.
print("After Multiplication Age is $age");
age/= 2; //Here age/=2 means age = age / 2.
print("After Division Age is $age");
}
Comparison Operators
It is used to compare values.
==
is used to check equal to.!=
is used to check not equal to.>
is used to check greater than.<
is used to check less than.>=
is used to check greater than or equal to.<=
is used to check less than or equal to.
void main() {
int num1=10;
int num2=5;
//printing info
print(num1==num2);
print(num1<num2);
print(num1>num2);
print(num1<=num2);
print(num1>=num2);
}
Logical Operators
It is used to compare values.
&&
This is ‘and’. This will return true if all conditions are true.||
This is ‘or’. This will return true if one of the statements is true.!
This is ’not’. This will return false if the result is true.
void main(){
int userid = 123;
int userpin = 456;
// Printing Info
print((userid == 123) && (userpin== 456)); // print true
print((userid == 1213) && (userpin== 456)); // print false.
print((userid == 123) || (userpin== 456)); // print true.
print((userid == 1213) || (userpin== 456)); // print true
print((userid == 123) != (userpin== 456));//print false
}