Dart Basic- Operators

Operators in Dart

Every expression is composed of operators and operands. Consider the equation “3 + 3”. Here “+” is an operator, and “3,3” are operands.

 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 arithmetic operators in the Dart programming language are:

  • +(Addition)
  • -(Subtraction)
  • %(Modulo)
  • *(Multiplication)
  • /(Division)
  • ++(Increment)
  • –(Decrement)
  • ~/(Divide,return an int value)

Example:

void main() {
  int num1 = 10;
  int num2 = 3;

  print("Addition: ${num1 + num2}");
  print("Subtraction: ${num1 - num2}");
  print("Multiplication: ${num1 * num2}");
  print("Division: ${num1 / num2}");
  print("Modulus: ${num1 % num2}");
}

Equality and Relational Operators

Relational Operators test or define the kind of relationship between two entities.

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

Example:

void main() {
  int num1 = 10;
  int num2 = 3;

  print("num1 == num2: ${num1 == num2}");
  print("num1 != num2: ${num1 != num2}");
  print("num1 > num2: ${num1 > num2}");
  print("num1 < num2: ${num1 < num2}");
  print("num1 >= num2: ${num1 >= num2}");
  print("num1 <= num2: ${num1 <= num2}");
}

Type test Operators

These operators are handy for checking types at runtime.

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

Example:

void main() {
  var num1 = 10;

  if (num1 is int) {
    print("num1 is an integer");
  }

  if (num1 is! double) {
    print("num1 is not a double");
  }
}

Bitwise Operators

The following table lists the bitwise operators available in Dart and their roles −

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 an in binary representation b (< 32) bits to the left, shifting in zeroes from the right.
Signpropagating right shifta ≫ bShifts an in binary representation b (< 32) bits to the right, discarding bits shifted off.

Example:

void main() {
  int num1 = 5;
  int num2 = 3;

  print("Bitwise AND: ${num1 & num2}");
  print("Bitwise OR: ${num1 | num2}");
  print("Bitwise XOR: ${num1 ^ num2}");
  print("Bitwise NOT: ${~num1}");
  print("Left shift: ${num1 << 2}");
  print("Right shift: ${num1 >> 1}");
}

Assignment Operators

The following table lists the assignment operators available in Dart.

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:

void main() {
  int a = 5;
  int b = 2;
  
  // Assign the value of b to a and increment b by 1
  a = b++;
  print("a = $a, b = $b"); // Output: a = 2, b = 3
  
  // Assign the value of b to a and decrement b by 1
  a = b--;
  print("a = $a, b = $b"); // Output: a = 3, b = 2
  
  // Add 2 to a and assign the result to a
  a += 2;
  print("a = $a"); // Output: a = 5
  
  // Subtract 1 from a and assign the result to a
  a -= 1;
  print("a = $a"); // Output: a = 4
  
  // Multiply a by 2 and assign the result to a
  a *= 2;
  print("a = $a"); // Output: a = 8
  
  // Divide a by 2 and assign the result to a
  a ~/= 2;
  print("a = $a"); // Output: a = 4
  
  // Take the remainder when a is divided by 3 and assign the result to a
  a %= 3;
  print("a = $a"); // Output: a = 1
  
  // Shift the bits of a one position to the left and assign the result to a
  a <<= 1;
  print("a = $a"); // Output: a = 2
  
  // Shift the bits of a one position to the right and assign the result to a
  a >>= 1;
  print("a = $a"); // Output: a = 1
  
  // Bitwise AND of a and 3, and assign the result to a
  a &= 3;
  print("a = $a"); // Output: a = 1
  
  // Bitwise OR of a and 2, and assign the result to a
  a |= 2;
  print("a = $a"); // Output: a = 3
  
  // Bitwise XOR of a and 3, and assign the result to a
  a ^= 3;
  print("a = $a"); // Output: a = 0
}

Logical Operators

Logical operators are used to combining two or more conditions.

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.

Example:

void main() {
  bool a = true;
  bool b = false;

  // Logical AND operator
  print(a && b); // Output: false

  // Logical OR operator
  print(a || b); // Output: true

  // Logical NOT operator
  print(!a); // Output: false
}

Conditional Expressions

Dart has two operators that let you evaluate expressions that might otherwise require if-else statements −

condition? expr1 : expr2

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

Example:

void main() {
  int x = 10;
  int y = 20;

  // using ternary operator
  int result1 = x > y ? x : y;
  print("Result 1: $result1");

  // using if-else statement
  int result2;
  if (x > y) {
    result2 = x;
  } else {
    result2 = y;
  }
  print("Result 2: $result2");

  // using null-aware operator
  String name;
  String displayName = name ?? "User";
  print("Display Name: $displayName");
}

Output:

Result 1: 20
Result 2: 20
Display Name: User
Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.