Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR DART

operators in dart

Dart supports the following types of operators.

1.Arithmetic Operators
2.Assignment Operators
3.Relational Operators
4.Type test Operators
5.Logical Operators
6.Bitwise Operator
7.Conditional Operators
8.Casecade notation(..) Operators


1.Dart Arithmetic Operators

Example -
void main(){  
  print("Example of Assignment operators");  
  var n1 = 10;  
  var n2 = 5;  
    
  print("n1+n2 = ${n1+n2}");  
  print("n1-n2 = ${n1-n2}");  
  print("n1*n2 = ${n1*n2}");  
  print("n1/=n2 = ${n1/n2}");   
  print("n1%n2 = ${n1%n2}");     
} 

Output:
Example of Arithmetic operators 
n1+n2 = 15 
n1-n2 = 5 
n1*n2 = 50 
n1/=n2 = 2 
n1%n2 = 0

2. Unary Operators (post and pre)

Example -
void main() {   
   var x = 30;   
   print(x++);                  //The postfix value  
     
var y = 25;  
print(++y);                 //The prefix value,  
         
var z = 10;  
print(--z);                  //The prefix value  
  
var u = 12;                                           
   print(u--);     //The postfix value  
}          
   
Output:
30
26
9
12

3. Assignment Operator

Example -
void main(){  
 print("Example of Assignment operators");  
    
  var n1 = 10;  
  var n2 = 5;  
    
  n1+=n2;  
  print("n1+=n2 = ${n1}");  
    
  n1-=n2;  
  print("n1-=n2 = ${n1}");  
    
  n1*=n2;  
  print("n1*=n2 = ${n1}");  
    
  n1~/=n2;  
  print("n1~/=n2 = ${n1}");  
  n1%=n2;  
  print("n1%=n2 = ${n1}");    
}  

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

4. Relational Operator

Example -
void main() {   
var a = 30;  
var b = 20;  
  
print("The example of Relational Operator");  
  
var res = a>b;  
print("a is greater than b: "+res. toString());  // We will learn the toString in next tutorial  
  
var res0 = a<b;  
print("a is less than b: "+res0. toString());  
  
var res1 = a>=b;  
print("a is greater than or equal to b: "+res1. toString());  
  
var res2 = a<=b;  
print("a is less than and equal to b: "+res2. toString());  
  
var res3 = a!= b;  
print("a is not equal to  b: "+res3. toString());  
  
var res4 = a==b;  
print("a is  equal to  b: "+res4. toString());  
}  

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

5. Type Test Operators

Example:
void main()  
{  
  var num = 10;  
  var name = "JavaTpoint";  
  print(num is int);    
  print(name is! String );  
} 

Output:
true
false

6. Logical Operators

Example:
void main(){  
  bool bool_val1 = true, bool_val2 = false;   
  print("Example of the logical operators");  
    
  var val1 = bool_val1 && bool_val2;  
  print(val1);  
    
  var val2 = bool_val1 || bool_val2;  
  print(val2);  
    
  var val3 = !(bool_val1 || bool_val2);  
  print(val3);   
}  

Output:
false 
true 
false

7. Bitwise Operators

Example -
void main(){  
  print("Example of Bitwise operators");  
    
  var a  = 25;  
  var b = 20;  
  var c = 0;  
    
  // Bitwise AND Operator  
  print("a & b = ${a&b}");  
    
  // Bitwise OR Operator  
  print("a | b = ${a|b}");  
    
  // Bitwise XOR  
  print("a ^ b = ${a^b}");  
    
  // Complement Operator  
  print("~a = ${(~a)}");  
    
  // Binary left shift Operator  
  c = a <<2;  
  print("c<<1= ${c}");  
    
  // Binary right shift Operator  
  c = a >>2;  
  print("c>>1= ${c}");  
}  

Output:
a & b = 16
 a | b = 29
 a ^ b = 13
 ~a = 4294967270
 c<<1= 100
 c>>1= 6

8. Conditional Operators (?:)

Example -
void main() {   
   var x = null;   
   var y = 20;   
   var val = x ?? y;   
   print(val);   
}  

Output:
20
 
PREVIOUS NEXT
Tagged: #operators #dart
ADD COMMENT
Topic
Name
3+1 =