Dart Operators

 

Dart Operators

An operator is a symbol that is used to manipulating the values or performs operations on its operand. The given expression: 5+4, in this expression, 5 and 4 are operands and "+" is the operator.

Dart provides an extensive set of built-in operators to accomplish various types of operations. Operators can be unary or binary, which means unary take only on operand and binary take two operands with operators. There are several types of operators. Following is the list of Dart Operators.

Types of Operators

Dart

supports the following types of operators.

  • Arithmetic Operators
  • Assignment Operators
  • Relational Operators
  • Type test Operators
  • Logical Operators
  • Bitwise Operator
  • Conditional Operators
  • Casecade notation(..) Operators
Dart Operators

Dart Arithmetic Operators

Arithmetic Operators are the most common operators that are used to perform addition, subtraction, multiplication, divide, etc. Let's take variable a holds 20 and variable b hold 10, then -


Sr.Operator NameDescriptionExample
1.Addition(+)It adds the left operand to the right operand.a+b will return 30
2.Subtraction(-)It subtracts the right operand from the left operand.a-b will return 10
3Divide(/)It divides the first operand by the second operand and returns quotient.a/b will return 2.0
4.Multiplication(*)It multiplies the one operand to another operand.a*b will return 200
5.Modulus(%)It returns a reminder after dividing one operand to another.a%b will return 0
6.Division(~/)It divides the first operand by the second operand and returns integer quotient.a/b will return 2
7.Unary Minus(-expr)It is used with a single operand changes the sign of it.-(a-b) will return -10

Example -

  1. void main(){  
  2.   print("Example of Assignment operators");  
  3.   var n1 = 10;  
  4.   var n2 = 5;  
  5.     
  6.   print("n1+n2 = ${n1+n2}");  
  7.   print("n1-n2 = ${n1-n2}");  
  8.   print("n1*n2 = ${n1*n2}");  
  9.   print("n1/=n2 = ${n1/n2}");   
  10.   print("n1%n2 = ${n1%n2}");     
  11. }  

Output:

Example of Arithmetic operators 
n1+n2 = 15 
n1-n2 = 5 
n1*n2 = 50 
n1/=n2 = 2 
n1%n2 = 0
  • Dart Unary Operators (post and pre)

In Java

, there are ++ and -- operators are known as increment and decrement operators and also known as unary operators, respectively. Unary operators, operate on single operand where ++ adds 1 to operands and -- subtract 1 to operand respectively.

The unary operators can be used in two ways - postfix and prefix. If ++ is used as a postfix(like x++), it returns the value of operand first then increments the value of x. If -- is used as a prefix(like ++x), it increases the value of x.

Sr.Operator NameDescriptionExample
1.++(Prefix)It increment the value of operand.++x
2.++(Postfix)It returns the actual value of operand before increment.x++
3.--(Prefix)It decrement the value of the operand.--x
4.--(Postfix)It returns the actual value of operand before decrement.x--

Let's understand the following example -

Example -

  1. void main() {   
  2.    var x = 30;   
  3.    print(x++);                  //The postfix value  
  4.      
  5. var y = 25;  
  6. print(++y);                 //The prefix value,  
  7.          
  8. var z = 10;  
  9. print(--z);                  //The prefix value  
  10.   
  11. var u = 12;                                           
  12.    print(u--);    }           //The postfix value  

Output:

30
26
9
12

Assignment Operator

Assignment operators are used to assigning value to the variables. We can also use it combined with the arithmetic operators. The list of assignment operators is given below. Suppose a holds value 20 and b holds 10.

OperatorsNameDescription
= (Assignment Operator)It assigns the right expression to the left operand.
+=(Add and Assign)It adds right operand value to the left operand and resultant assign back to the left operand. For example - a+=b → a = a+b → 30
-=(Subtract and Assign)It subtracts right operand value from left operand and resultant assign back to the left operand. For example - a-=b → a = a-b → 10
*=(Multiply and Assign)It multiplies the operands and resultant assign back to the left operand. For example - a*=b → a = a*b → 200
/=(Divide and Assign)It divides the left operand value by the right operand and resultant assign back to the left operand. For example - a%=b → a = a%b → 2.0
~/=(Divide and Assign)It divides the left operand value by the right operand and integer remainder quotient back to the left operand. For example - a%=b → a = a%b → 2
%=(Mod and Assign)It divides the left operand value by the right operand and remainder assign back to the left operand. For example - a%=b → a = a%b → 0
<<=(Left shift AND assign)The expression a<<=3 is equal to a = a<<3
>>=(Right shift AND assign)The expression a>>=3 is equal to a = a>>3
&=(Bitwise AND assign)The expression a&=3 is equal to a = a&3
^=(Bitwise exclusive OR and assign)The expression a^=3 is equal to a = a^3
|=(Bitwise inclusive OR and assign)The expression a|=3 is equal to a = a|3

Let's understand the following example -

Example -

  1. void main(){  
  2.  print("Example of Assignment operators");  
  3.     
  4.   var n1 = 10;  
  5.   var n2 = 5;  
  6.     
  7.   n1+=n2;  
  8.   print("n1+=n2 = ${n1}");  
  9.     
  10.   n1-=n2;  
  11.   print("n1-=n2 = ${n1}");  
  12.     
  13.   n1*=n2;  
  14.   print("n1*=n2 = ${n1}");  
  15.     
  16.   n1~/=n2;  
  17.   print("n1~/=n2 = ${n1}");  
  18.   n1%=n2;  
  19.   print("n1%=n2 = ${n1}");    
  20. }  

Output:

Example of Assignment operators 
n1+=n2 = 15 
n1-=n2 = 10 
n1*=n2 = 50 
n1~/=n2 = 10 
n1%=n2 = 0 

Relational Operator

Relational operators or Comparison operators are used to making a comparison between two expressions and operands. The comparison of two expressions returns the Boolean true and false. Suppose a holds 20 and b hold 10 then consider the following table.

Sr.OperatorDescription
1.>(greater than)a>b will return TRUE.
2.<(less than)a<b will return FALSE.
3.>=(greater than or equal to)a>=b will return TRUE.
4.<=(less than or equal to)a<=b will return FALSE.
5.==(is equal to)a==b will return FALSE.
6.!=(not equal to)a!=b will return TRUE.

Let's understand the following example -

Example -

  1. void main() {   
  2. var a = 30;  
  3. var b = 20;  
  4.   
  5. print("The example of Relational Operator");  
  6.   
  7. var res = a>b;  
  8. print("a is greater than b: "+res. toString());  // We will learn the toString in next tutorial  
  9.   
  10. var res0 = a<b;  
  11. print("a is less than b: "+res0. toString());  
  12.   
  13. var res1 = a>=b;  
  14. print("a is greater than or equal to b: "+res1. toString());  
  15.   
  16. var res2 = a<=b;  
  17. print("a is less than and equal to b: "+res2. toString());  
  18.   
  19. var res3 = a!= b;  
  20. print("a is not equal to  b: "+res3. toString());  
  21.   
  22. var res4 = a==b;  
  23. print("a is  equal to  b: "+res4. toString());  
  24. }  

Output:

The example of Relational Operator
a is greater than b: true
a is less than b: false
a is greater than or equal to b: true
a is less than and equal to b: false
a is not equal to  b: true
a is  equal to  b: false

Dart Type Test Operators

The Type Test Operators are used to testing the types of expressions at runtime. Consider the following table.

Sr.OperatorDescription
1.asIt is used for typecast.
2.isIt returns TRUE if the object has specified type.
3.is!It returns TRUE if the object has not specified type.

Let's understand the following example.

  1. void main()  
  2. {  
  3.   var num = 10;  
  4.   var name = "JavaTpoint";  
  5.   print(num is int);    
  6.   print(name is! String );  
  7. }  

Output:

true
false

Dart Logical Operators

The Logical Operators are used to evaluate the expressions and make the decision. Dart supports the following logical operators.

Sr.OperatorDescription
1.&&(Logical AND)It returns if all expressions are true.
2.||(Logical OR)It returns TRUE if any expression is true.
3.!(Logical NOT)It returns the complement of expression.

Let's understand the following example.

  1. void main(){  
  2.   bool bool_val1 = true, bool_val2 = false;   
  3.   print("Example of the logical operators");  
  4.     
  5.   var val1 = bool_val1 && bool_val2;  
  6.   print(val1);  
  7.     
  8.   var val2 = bool_val1 || bool_val2;  
  9.   print(val2);  
  10.     
  11.   var val3 = !(bool_val1 || bool_val2);  
  12.   print(val3);   
  13. }  

Output:

Example of the logical operators 
false 
true 
false

Dart Bitwise Operators

The Bitwise operators perform operation bit by bit on the value of the two operands. Following is the table of bitwise operators.

Let's understand the following example.

  1. If a = 7  
  2. b = 6  
  3. then binary(a) = 0111  
  4. binary(b) = 0011  
  5. Hence a & b = 0011, a|b = 0111 and a^b = 0100  

Sr.OperatorsDescription
1.&(Binary AND)It returns 1 if both bits are 1.
2.|(Binary OR)It returns 1 if any of bit is 1.
3.^(Binary XOR)It returns 1 if both bits are different.
4.~(Ones Compliment)It returns the reverse of the bit. If bit is 0 then the compliment will be 1.
5.<<(Shift left)The value of left operand moves left by the number of bits present in the right operand.
6.>>(Shift right)The value of right operand moves left by the number of bits present in the left operand.

Let's understand the following example -

  1. void main(){  
  2.   print("Example of Bitwise operators");  
  3.     
  4.   var a  = 25;  
  5.   var b = 20;  
  6.   var c = 0;  
  7.     
  8.   // Bitwise AND Operator  
  9.   print("a & b = ${a&b}");  
  10.     
  11.   // Bitwise OR Operator  
  12.   print("a | b = ${a|b}");  
  13.     
  14.   // Bitwise XOR  
  15.   print("a ^ b = ${a^b}");  
  16.     
  17.   // Complement Operator  
  18.   print("~a = ${(~a)}");  
  19.     
  20.   // Binary left shift Operator  
  21.   c = a <<2;  
  22.   print("c<<1= ${c}");  
  23.     
  24.   // Binary right shift Operator  
  25.   c = a >>2;  
  26.   print("c>>1= ${c}");  
  27. }  

Output:

Example of Bitwise operators 
a & b = 16
 a | b = 29
 a ^ b = 13
 ~a = 4294967270
 c<<1= 100
 c>>1= 6

Dart Conditional Operators (?:)

The Conditional Operator is same as if-else statement and provides similar functionality as conditional statement. It is the second form of if-else statement. It is also identified as "Ternary Operator". The syntax is given below.

Syntax 1 -

  1. condition ? exp1 : exp2  

If the given condition is TRUE then it returns exp1 otherwise exp2.

Syntax 2 -

  1. exp1 ?? expr2  

If the exp1 is not-null, returns its value, otherwise returns the exp2's value.

Let's understand the following example.

Example - 1

  1. void main() {   
  2.    var x = null;   
  3.    var y = 20;   
  4.    var val = x ?? y;   
  5.    print(val);   
  6. }  

Output:

20

Let's have a look at another scenario.

Example -2

  1. void main() {   
  2.    var a = 30;   
  3.    var output = a > 42 ? "value greater than 10":"value lesser than equal to 30";   
  4.    print(output);   
  5. }  

Output:

value lesser than or equal to 30

Dart Cascade notation Operators

The Cascade notation Operators (..) is used to evaluate a series of operation on the same object. It is an identical as the method chaining that avoids several of steps, and we don't need store results in temporary variables

Comments