DART – Operators

0 0
Read Time:3 Minute, 46 Second

DART – Operators

An operator is a symbol that tells of performing specific mathematical, relational or logical operations.

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 out operations on one or two operands. 

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
  • Cascade notation

Arithmetic Operators

Arithmetic operators perform addition, subtraction, multiplication, division, etc.

Operator NameDescription
1.Addition(+)It adds the left operand to the right operand.
2.Subtraction(-)It subtracts the right operand from the left operand.
3Divide(/)It divides the first operand by the second operand and returns the quotient.
4.Multiplication(*)It multiplies one operand to another operand.
5.Modulus(%)It returns a reminder after dividing one operand into another.
6.Division(/)It divides the first operand by the second operand and returns an integer quotient.
7.Unary Minus(-expr)It is used with a single operand that changes its sign of it.
void main(){    
  var a = 10;  
  var b = 5;  
    
  print("a+b = ${a+b}");  
  print("a-b = ${a-b}");  
  print("a*b = ${a*b}");  
  print("a/b = ${a/b}");   
  print("a%b = ${a%b}");     
}  

Assignment Operator

Assignment operators are used to assign value to the variables.

OperatorsName
= (Assignment Operator)It assigns the right expression to the left operand.
+=(Add and Assign)It adds the right operand value to the left operand and the resultant assigned it back to the left operand. For example – a+=b → a = a+b → 30
-=(Subtract and Assign)It subtracts the right operand value from the left operand and the resultant assigns it back to the left operand. For example – a-=b → a = a-b → 10
*=(Multiply and Assign)It multiplies the operands and resultant assigns them 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 the resultant assigned 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 the remainder assigns it back to the left operand. For example – a%=b → a = a%b → 0
void main(){  
  var a = 10;  
  var b= 5;  
    
  a+=b;  
  print("a+=b= ${a}");  
    
  a-=b;  
  print("a-=b= ${a}");  
    
  a*=b;  
  print("a*=b = ${a}");  
    
  a~/=b;  
  print("a~/=b= ${a}");  
  a%=b;  
  print("a%=b= ${a}");    
} 

Relational Operator

Relational operators or Comparison operators are used to make a comparison between two expressions and operands. The comparison of two expressions returns the Boolean true and false.

OperatorMeaning
==Equal; see discussion below
!=Not equal
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Type test operators

The asis, and is! operators are handy for checking types at runtime.

OperatorMeaning
asTypecast (also used to specify library prefixes)
isTrue if the object has the specified type
is!True if the object doesn’t have the specified type

Logical operators

You can invert or combine boolean expressions using logical operators.

OperatorMeaning
!exprinverts the following expression (changes false to true, and vice versa)
||logical OR
&&logical AND

Dart Bitwise Operators

The Bitwise operators perform operations bit by bit on the value of the two operands. 

OperatorMeaning
&AND
|OR
^XOR
~exprUnary bitwise complement (0s become 1s; 1s become 0s)
<<Shift left
>>Shift right
>>>Unsigned shift right

Dart Conditional Operators (?:)

The Conditional Operator is the same as the if-else statement and provides similar functionality as a conditional statement. It is the second form of an if-else statement. It is also identified as a “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.

Cascade notation

Cascades (..?..) allow you to make a sequence of operations on the same object. In addition to accessing instance members, you can also call instance methods on that same object. This often saves you the step of creating a temporary variable and allows you to write more fluid code.

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
100 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Comment

Verified by MonsterInsights