Operators in Dart
In this lesson, you will learn about operators in Dart. Operators are used to perform calculations, comparisons, and logical decisions on data values.
Operators are heavily used in real applications such as billing systems, analytics dashboards, authentication logic, and data processing.
What Are Operators?
Operators are special symbols that tell Dart to perform an action. They work with one or more values called operands.
For example, adding prices, comparing scores, or checking login status all require operators.
Types of Operators in Dart
- Arithmetic Operators
- Relational (Comparison) Operators
- Logical Operators
- Assignment Operators
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations. They are commonly used for prices, totals, scores, and analytics.
+Addition-Subtraction*Multiplication/Division%Modulus (remainder)
double price = 499.99;
int quantity = 3;
double totalCost = price * quantity;
print(totalCost);
This example calculates the total cost of items in a shopping cart.
Using Modulus Operator (%)
The modulus operator returns the remainder of a division. It is useful for checking even/odd numbers or cyclic logic.
int students = 27;
print(students % 2);
If the result is 0, the number is even. Otherwise, it is odd.
Relational (Comparison) Operators
Relational operators compare two values and return a boolean result. They are commonly used in conditions and validations.
==Equal to!=Not equal to>Greater than<Less than>=Greater than or equal to<=Less than or equal to
int examScore = 72;
print(examScore >= 50);
This checks whether a student has passed an exam.
Logical Operators
Logical operators are used to combine multiple conditions. They are very common in login systems and access control.
&&AND||OR!NOT
bool isLoggedIn = true;
bool isAdmin = false;
print(isLoggedIn && isAdmin);
print(isLoggedIn || isAdmin);
This logic controls whether a user can access restricted features.
Assignment Operators
Assignment operators are used to assign or update variable values. They make code shorter and more readable.
=Assign+=Add and assign-=Subtract and assign*=Multiply and assign
int points = 10;
points += 5;
points -= 2;
print(points);
This is commonly used in scoring systems and counters.
Operator Precedence
Operator precedence determines the order in which operations are performed. Multiplication and division happen before addition and subtraction.
int result = 10 + 5 * 2;
print(result);
The multiplication is performed first, resulting in 20.
📝 Practice Exercises
Exercise 1
Calculate the total price for 4 items costing 299.50 each.
Exercise 2
Check if a user age is greater than or equal to 18.
Exercise 3
Use logical operators to check if a user is logged in and verified.
✅ Practice Answers
Answer 1
double total = 299.50 * 4;
print(total);
Answer 2
int age = 21;
print(age >= 18);
Answer 3
bool loggedIn = true;
bool verified = true;
print(loggedIn && verified);
What’s Next?
In the next lesson, you will learn about conditional statements in Dart.
You will see how operators are combined with conditions to control real application behavior.