Dart Programming – Operators

  • Post author:
  • Post category:Dart
  • Post comments:1 Comment
dart programming operators

In this guide, we will discuss Dart Programming Operators. An expression is a special kind of statement that evaluates to a value. Every expression is composed of βˆ’

  • Operands βˆ’ Represents the data
  • Operator βˆ’ Defines how the operands will be processed to produce a value.

Consider the following expression – “2 + 3”. In this expression, 2 and 3 are operands and the symbol “+” (plus) is the operator.

In this chapter, 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 Operators

The following table shows the arithmetic operators supported by Dart.

Show Examples

Sr.NoOperators & Meaning
1+
Add
2βˆ’
Subtract
3-expr
Unary minus, also known as negation (reverse the sign of the expression)
4*
Multiply
5/
Divide
6~/
Divide, returning an integer result
7%
Get the remainder of an integer division (modulo)
8++
Increment
9
Decrement

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.

Assume the value of A is 10 and B is 20.

Show Examples

OperatorDescriptionExample
>Greater than(A > B) is False
<Lesser than(A < B) is True
>=Greater than or equal to(A >= B) is False
<=Lesser than or equal to(A <= B) is True
==Equality(A==B) is False
!=Not equal(A!=B) is True

Type test Operators

These operators are handy for checking types at runtime.

Show Examples

OperatorMeaning
isTrue if the object has the specified type
is!False if the object has the specified type

Bitwise Operators

The following table lists the bitwise operators available in Dart and their role βˆ’

Show Examples

OperatorDescriptionExample
Bitwise ANDa & bReturns a one in each bit position for which the corresponding bits of both operands are ones.
Bitwise ORa | bReturns a one in each bit position for which the corresponding bits of either or both operands are ones.
Bitwise XORa ^ bReturns a one in each bit position for which the corresponding bits of either but not both operands are ones.
Bitwise NOT~ aInverts the bits of its operand.
Left shifta β‰ͺ bShifts a in binary representation b (< 32) bits to the left, shifting in zeroes from the right.
Signpropagating right shifta ≫ bShifts a in binary representation b (< 32) bits to the right, discarding bits shifted off.

Assignment Operators

The following table lists the assignment operators available in Dart.

Show Examples

Sr.NoOperator & Description
1=(Simple Assignment )
Assigns values from the right side operand to the left side operandEx:C = A + B will assign the value of A + B into C
2??=
Assign the value only if the variable is null
3+=(Add and Assignment)
It adds the right operand to the left operand and assigns the result to the left operand.Ex: C += A is equivalent to C = C + A
4─=(Subtract and Assignment)
It subtracts the right operand from the left operand and assigns the result to the left operand.Ex: C -= A is equivalent to C = C – A
5*=(Multiply and Assignment)
It multiplies the right operand with the left operand and assigns the result to the left operand.Ex: C *= A is equivalent to C = C * A
6/=(Divide and Assignment)
It divides the left operand with the right operand and assigns the result to the left operand.

Note βˆ’ Same logic applies to Bitwise operators, so they will become β‰ͺ=, ≫=, ≫=, ≫=, |= and ^=.

Logical Operators

Logical operators are used to combine two or more conditions. Logical operators return a Boolean value. Assume the value of variable A is 10 and B is 20.

Show Examples

OperatorDescriptionExample
&&And βˆ’ The operator returns true only if all the expressions specified return true(A > 10 && B > 10) is False.
||OR βˆ’ The operator returns true if at least one of the expressions specified return true(A > 10 || B > 10) is True.
!NOT βˆ’ The operator returns the inverse of the expression’s result. For E.g.: !(7>5) returns false!(A > 10) is True.

Conditional Expressions

Dart has two operators that let you evaluate expressions that might otherwise require ifelse statements βˆ’

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.

expr1 ?? expr2

If expr1 is non-null, returns its value; otherwise, evaluates and returns the value of expr2

Example

The following example shows how you can use conditional expression in Dart βˆ’

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

It will produce the following output βˆ’

value lesser than or equal to 10

Example

Let’s take another example βˆ’

void main() { 
   var a = null; 
   var b = 12; 
   var res = a ?? b; 
   print(res); 
}

It will produce the following output βˆ’

12

Next Topic : Click Here

This Post Has One Comment

Leave a Reply