Operators in Dart

Operators are symbols that perform operations on operands. Operands can be variables, constants, or values. Dart supports a variety of operators, classified into different categories based on their functionality.

In the below PDF we discuss about Operators in detail in simple language, Hope this will help in better understanding.

Types of Operators in Dart:

1. Arithmetic Operators:
Arithmetic operators are used to perform mathematical calculations on numerical values. Dart supports standard arithmetic operators:

  • Addition (+): Adds two operands.
  • Subtraction (-): Subtracts the second operand from the first.
  • Multiplication (*): Multiplies two operands.
  • Division (/): Divides the first operand by the second.
  • Remainder (%): Returns the remainder of the division operation.

Example:

void main() {
int a = 10;
int b = 5;

print(a + b); // Output: 15
print(a - b); // Output: 5
print(a * b); // Output: 50
print(a / b); // Output: 2.0
print(a % b); // Output: 0
}

2. Relational Operators:
Relational operators are used to compare values. They return a boolean value indicating whether the comparison is true or false.

  • Equal (==): Checks if two operands are equal.
  • Not equal (!=): Checks if two operands are not equal.
  • Greater than (>): Checks if the left operand is greater than the right.
  • Less than (<): Checks if the left operand is less than the right.
  • Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right.
  • Less than or equal to (<=): Checks if the left operand is less than or equal to the right.

Example:

void main() {
int a = 10;
int b = 5;

print(a == b); // Output: false
print(a != b); // Output: true
print(a > b); // Output: true
print(a < b); // Output: false
print(a >= b); // Output: true
print(a <= b); // Output: false
}

3. Logical Operators:
Logical operators are used to combine multiple conditions and return a boolean result.

  • AND (&&): Returns true if both operands are true.
  • OR (||): Returns true if at least one operand is true.
  • NOT (!): Returns the opposite of the operand’s boolean value.

Example:

void main() {
bool x = true;
bool y = false;

print(x && y); // Output: false
print(x || y); // Output: true
print(!x); // Output: false
}

4. Assignment Operators:
Assignment operators are used to assign values to variables.

  • Assignment (=): Assigns the value of the right operand to the left operand.
  • Compound Assignment (+=, -=, *=, /=, %=): Performs the operation and assigns the result to the left operand.

Example:

void main() {
int a = 10;
int b = 5;

a += b; // Equivalent to: a = a + b;
print(a); // Output: 15
}

5. Bitwise Operators:
Bitwise operators perform operations on individual bits of integer operands.

  • Bitwise AND (&): Performs a bitwise AND operation.
  • Bitwise OR (|): Performs a bitwise OR operation.
  • Bitwise XOR (^): Performs a bitwise XOR (exclusive OR) operation.
  • Bitwise NOT (~): Flips the bits of the operand.
  • Left Shift (<<): Shifts the bits of the operand to the left.
  • Right Shift (>>): Shifts the bits of the operand to the right.

Example:

void main() {
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011

print(a & b); // Output: 1 (Binary: 0001)
print(a | b); // Output: 7 (Binary: 0111)
print(a ^ b); // Output: 6 (Binary: 0110)
print(~a); // Output: -6 (Binary: 1111 1111 1111 1010)
print(a << 1); // Output: 10 (Binary: 1010)
print(a >> 1); // Output: 2 (Binary: 0010)
}

Conclusion:

In conclusion, Operators are fundamental elements of any programming language, and Dart offers a comprehensive set of operators to perform various operations efficiently. By understanding and utilizing these operators effectively, you can write cleaner and more concise code in Dart. Whether you’re working on simple arithmetic calculations or complex bitwise operations, Dart’s operators provide the flexibility and power you need to accomplish your programming tasks.

Related Question

Operators in Dart are special symbols or keywords that perform operations on operands, such as variables, literals, or expressions.

Operators in Dart can be categorized into several types: arithmetic, relational, logical, bitwise, assignment, conditional, and others.

Arithmetic operators in Dart are used to perform basic mathematical operations like addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).

No, Dart does not support operator overloading. Each operator has a fixed set of behaviors defined by the language, and these behaviors cannot be modified or extended.

Null-aware operators in Dart provide concise ways to handle null values, reducing the need for verbose null-checking code. Examples include the null-aware access (?.) and null-aware cascade (..?) operators.

Relevant

OOPS in Dart Object-Oriented Programming

Queue in Dart Programming A

Maps in Dart A Map

Set in Dart A Set

Dart Lists A Dart list

Strings in Dart A Dart

Functions in Dart In Dart,

1 thought on “Operators in Dart”

Leave a Comment

Your email address will not be published. Required fields are marked *

//sticy ads