# Dart Basic- Operators

Home /

## 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:

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

Example:

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

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.

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.

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 −

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.

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.

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
``````