]

Dart Programming - Operators

Published May 07, 2020

Dart provides some relatively simple operators to simplify the operation, most of them are the same as Java, the following introduces a few different operators

  • Arithmetic operator
  • Relational operator
  • Logical Operators
  • Assignment operator
  • Conditional expression

Arithmetic operator

Sr.No Operators & 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

 

Arithmetic Operators Example

int a = 2;
int b = 10;


  print(a + b); //12
  print(a - b);//-8
  print(a * b);//20
  print(a / b);//0.2
  print(a ~/ b);//0
  print(a % b);//2

  print(a++);// 2 
  
  print(++a); //3 
  

  print(a--);//4
  print(--a);//2

 

Relational operator

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

Operator Description Example
> 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

 

Relational operator Example

int a = 2;
int b = 10;
  print(a == b);//false
  print(a != b);//true
  print(a > b);//false
  print(a < b);//true
  print(a >= b);//false
  print(a <= b);//true

 

logic operation
Boolean operations

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

Operator Description Example
&&

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.

 

Example

 bool isTrue = true;
  bool isFalse = false;
  print(!isTrue);//false
  print(isTrue && isFalse);//false 
  print(isTrue || isFalse);

 

Assignment operators

The following table lists the assignment operators available in Dart.

Sr.No Operator & Description
1 =(Simple Assignment )

Assigns values from the right side operand to the left side operand

Ex: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.

 

Example

int m = 10;
  int n = 5;
  n ??= 10; 
  print(n);


  m += 2;
  print(m);//12

  m -= 2;
  print(m);//10

 

Conditional Expressions

1. Trinocular operator: condition? Expr1: expe2

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

2. ?? Operator: expr1 ?? expr2

If expr1 is non-null, return its value; otherwise, calculate and return the value of expr2

Example

int age =10;
  String str = age == 10 ? "10" :"20";
  print(str);


  String x ;
  String y = "java";

  String z = x ?? y;
  print(z);

 


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

270 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions