Dart Programming Part-2 : Operators

Jayesh Pansheriya
Analytics Vidhya
Published in
4 min readDec 30, 2019
Photo by Émile Perron on Unsplash

Every expression is composed of two parts:

  1. Operand — They represents data.
  2. Operator — An operator is a symbol that tells the compiler to perform a specific mathematical, relational, or logical operation and produce a final result.
  • For example, A + B where
  • Here A and B are operands and + is an operator.

we will discuss the operators that are available in Dart.

  • Arithmetic Operators
  • Equality and Relational Operators
  • Type test Operators
  • Bitwise Operators
  • Assignment Operators
  • Logical Operators

Arithmetic Operator

  • There are nine types of an arithmetic operator are there in a dart.
void main() { 
var num1 = 101;
var num2 = 2;
var res = 0;

res = num1+num2;
print("Addition: ${res}");

res = num1-num2;
print("Subtraction: ${res}");

res = num1*num2;
print("Multiplication: ${res}");

res = num1/num2;
print("Division: ${res}");

res = num1~/num2;
print("Division returning Integer: ${res}");

res = num1%num2;
print("Remainder: ${res}");

num1++;
print("Increment: ${num1}");

num2--;
print("Decrement: ${num2}");
}
Output:
Addition:103
Subtraction: 99
Multiplication: 202
Division: 50.5
Division returning Integer: 50
Remainder: 1
Increment: 102
Decrement: 1

Equality and Relational Operators

Relational Operators tests or defines the kind of relationship between two entities. Relational operators return a Boolean value i.e. true/ false.

void main() { 
var num1 = 5;
var num2 = 9;
var res = num1>num2;
print('num1 greater than num2 :: ' +res.toString());

res = num1<num2;
print('num1 lesser than num2 :: ' +res.toString());

res = num1 >= num2;
print('num1 greater than or equal to num2 :: ' +res.toString());

res = num1 <= num2;
print('num1 lesser than or equal to num2 :: ' +res.toString());

res = num1 != num2;
print('num1 not equal to num2 :: ' +res.toString());

res = num1 == num2;
print('num1 equal to num2 :: ' +res.toString());
}
Output:
num1 greater than num2 :: false
num1 lesser than num2 :: true
num1 greater than or equal to num2 :: false
num1 lesser than or equal to num2 :: true
num1 not equal to num2 :: true
num1 equal to num2 :: false

Type test Operators

These operators are handy for checking types at runtime.

void main() { 
int n = 2;
print(n is int);
}
Output:
true
--------------------------------------------------------------------
void main() {
double n = 2.20;
var num = n is! int;
print(num);
}
Output:
true

Logical Operators

Logical operators are used to combine two or more conditions. Logical operators return a Boolean value.

void main() {  
var a = 10;
var b = 12;
var res = (a<b)&&(b>10);
print(res);
}
Output:
true
--------------------------------------------------------------------
void main() {
var a = 10;
var b = 12;
var res = (a>b)||(b<10);

print(res);
var res1 =!(a==b);
print(res1);
}
Output:
false
true

Conditional Expressions

Dart has two special operators which can help you to evaluate small expressions that might require an entire if-else statement

condition ? expr1 : expr2

If condition is true, then the expression evaluates expr1 (and returns its value); otherwise, it evaluates and returns the value of expr2.

void main() { 
var a = 10;
var res = a > 12 ? "value greater than 10":"value lesser than or equal to 10";
print(res);
}
Output:
value lesser than or equal to 10

How Can You Contribute?

Posts In This Series

Show Your Support

Press the clap button below if you liked reading this post. The more you clap the more it motivates me to write better!

--

--