Dart Operators: Arithmetic, Assignment, Comparison, Logical, and Conditional Operators
Operators are special symbols or keywords used to perform operations on values and variables.
In Dart, operators are an important part of programming because they allow developers to perform
calculations, assign values, compare data, combine conditions, and make decisions.
Operators are part of the Dart programming fundamentals covered in JustAcademy's Flutter training,
along with variables, data types, control statements, functions, OOP, collections, and asynchronous
programming. :contentReference[oaicite:0]{index=0}
Learn more:
JustAcademy Flutter Training
Register for a demo:
JustAcademy Course Demo Registration
1. What Are Operators in Dart?
An operator performs an operation on one or more values. The values on which an operator works
are called operands.
int a = 10;
int b = 5;
int result = a + b;
In the above example:
a and b are operands.
+ is the operator.
result stores the calculated value.
2. Main Categories of Operators
| Operator Category |
Purpose |
Common Operators |
| Arithmetic |
Perform mathematical calculations |
+ - * / ~/ % |
| Assignment |
Assign or update values |
= += -= *= /= ~/= %= |
| Comparison |
Compare two values |
== != > < >= <= |
| Logical |
Combine or reverse conditions |
&& || ! |
| Conditional |
Choose between values based on a condition |
? :, ?? |
3. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations such as addition,
subtraction, multiplication, division, and remainder operations.
Arithmetic Operators in Dart
| Operator |
Name |
Example |
Result |
+ |
Addition |
10 + 5 |
15 |
- |
Subtraction |
10 - 5 |
5 |
* |
Multiplication |
10 * 5 |
50 |
/ |
Division |
10 / 5 |
2.0 |
~/ |
Integer division |
10 ~/ 3 |
3 |
% |
Remainder |
10 % 3 |
1 |
3.1 Addition Operator (+)
The + operator adds two values.
int price = 500;
int delivery = 50;
int total = price + delivery;
print(total); // 550
3.2 Subtraction Operator (-)
The - operator subtracts one value from another.
int total = 1000;
int discount = 200;
int finalPrice = total - discount;
print(finalPrice); // 800
3.3 Multiplication Operator (*)
The * operator multiplies two values.
int price = 250;
int quantity = 4;
int total = price * quantity;
print(total); // 1000
3.4 Division Operator (/)
The / operator performs division and produces a numeric result.
With integer operands, the result is a double.
int a = 10;
int b = 4;
double result = a / b;
print(result); // 2.5
3.5 Integer Division Operator (~/)
The ~/ operator performs integer division and returns the truncated
integer result.
int result = 10 ~/ 3;
print(result); // 3
3.6 Remainder Operator (%)
The % operator returns the remainder after division.
int remainder = 10 % 3;
print(remainder); // 1
Practical example: Checking whether a number is even or odd.
int number = 15;
if (number % 2 == 0) {
print("Even");
} else {
print("Odd");
}
4. Assignment Operators
Assignment operators are used to store values in variables or update the existing
value of a variable.
4.1 Basic Assignment (=)
The = operator assigns a value to a variable.
int age = 25;
String name = "Rahul";
print(age);
print(name);
4.2 Add and Assign (+=)
+= adds a value to the existing variable and assigns the new value back.
int score = 50;
score += 10;
print(score); // 60
This is equivalent to:
score = score + 10;
4.3 Subtract and Assign (-=)
int balance = 1000;
balance -= 250;
print(balance); // 750
This is equivalent to:
balance = balance - 250;
4.4 Multiply and Assign (*=)
int number = 10;
number *= 5;
print(number); // 50
4.5 Divide and Assign (/=)
double price = 1000;
price /= 2;
print(price); // 500.0
4.6 Integer Divide and Assign (~/=)
int number = 10;
number ~/= 3;
print(number); // 3
4.7 Remainder and Assign (%=)
int number = 17;
number %= 5;
print(number); // 2
Assignment Operator Summary
| Operator |
Example |
Equivalent |
= |
x = 10 |
Assign 10 |
+= |
x += 5 |
x = x + 5 |
-= |
x -= 5 |
x = x - 5 |
*= |
x *= 5 |
x = x * 5 |
/= |
x /= 5 |
x = x / 5 |
~/= |
x ~/= 5 |
x = x ~/ 5 |
%= |
x %= 5 |
x = x % 5 |
5. Comparison Operators
Comparison operators compare two values and return a Boolean result:
true or false.
Comparison Operators in Dart
| Operator |
Meaning |
Example |
== |
Equal to |
10 == 10 |
!= |
Not equal to |
10 != 5 |
> |
Greater than |
10 > 5 |
< |
Less than |
5 < 10 |
>= |
Greater than or equal to |
10 >= 10 |
<= |
Less than or equal to |
5 <= 10 |
5.1 Equal To (==)
int a = 10;
int b = 10;
print(a == b); // true
5.2 Not Equal To (!=)
int age = 20;
print(age != 18); // true
5.3 Greater Than (>)
int marks = 85;
print(marks > 50); // true
5.4 Less Than (<)
int age = 16;
print(age < 18); // true
5.5 Greater Than or Equal To (>=)
int marks = 60;
print(marks >= 60); // true
5.6 Less Than or Equal To (<=)
int age = 18;
print(age <= 18); // true
Comparison Example
int marks = 75;
if (marks >= 40) {
print("Student passed");
} else {
print("Student failed");
}
6. Logical Operators
Logical operators are used to combine multiple Boolean conditions or reverse a condition.
They are especially useful with if, else if, and other decision-making
statements.
Logical Operators in Dart
| Operator |
Name |
Purpose |
&& |
Logical AND |
All conditions must be true |
|| |
Logical OR |
At least one condition must be true |
! |
Logical NOT |
Reverses a Boolean value |
6.1 Logical AND (&&)
The && operator returns true only when both conditions are true.
int age = 25;
bool hasId = true;
if (age >= 18 && hasId) {
print("Allowed");
} else {
print("Not allowed");
}
Both conditions must be true:
true && true // true
true && false // false
false && true // false
false && false // false
6.2 Logical OR (||)
The || operator returns true when at least one condition is true.
bool isAdmin = false;
bool isManager = true;
if (isAdmin || isManager) {
print("Access granted");
}
Truth table:
true || true // true
true || false // true
false || true // true
false || false // false
6.3 Logical NOT (!)
The ! operator reverses a Boolean value.
bool isLoggedIn = false;
print(!isLoggedIn); // true
Another example:
bool isBlocked = false;
if (!isBlocked) {
print("User can continue");
}
Combining Logical Operators
int age = 25;
bool hasLicense = true;
bool hasExperience = true;
if (age >= 18 && hasLicense && hasExperience) {
print("Eligible");
} else {
print("Not eligible");
}
7. Conditional Operators
Conditional operators allow a program to choose a value based on a condition.
They are particularly useful for writing short decision-making expressions.
7.1 Ternary Operator (? :)
The ternary operator has this syntax:
condition ? valueIfTrue : valueIfFalse;
Example:
int age = 20;
String result = age >= 18 ? "Adult" : "Minor";
print(result); // Adult
The above code is a shorter version of:
int age = 20;
String result;
if (age >= 18) {
result = "Adult";
} else {
result = "Minor";
}
7.2 Ternary Operator with Marks
int marks = 75;
String result = marks >= 40 ? "Pass" : "Fail";
print(result); // Pass
7.3 Ternary Operator with Login Status
bool isLoggedIn = true;
String message = isLoggedIn
? "Welcome User"
: "Please Login";
print(message);
7.4 Null-Coalescing Operator (??)
The ?? operator provides a fallback value when a nullable expression is
null.
String? username;
String displayName = username ?? "Guest";
print(displayName); // Guest
Another example:
String? city = null;
String userCity = city ?? "Unknown City";
print(userCity);
7.5 Null-Coalescing Assignment (??=)
The ??= operator assigns a value only when the variable currently contains
null.
String? name;
name ??= "Guest";
print(name); // Guest
If the variable already contains a value, it is not replaced.
String? name = "Amit";
name ??= "Guest";
print(name); // Amit
8. Arithmetic Operators in a Shopping Cart
Operators are commonly used in real-world Flutter applications such as e-commerce apps,
expense trackers, and billing systems.
double productPrice = 500;
int quantity = 3;
double discount = 100;
double subtotal = productPrice * quantity;
double finalPrice = subtotal - discount;
print("Subtotal: ₹$subtotal");
print("Final Price: ₹$finalPrice");
9. Assignment Operators in a Shopping Cart
double total = 500;
total += 100;
total -= 50;
print(total); // 550
10. Comparison Operators in a Student Result
int marks = 78;
if (marks >= 90) {
print("Grade A+");
} else if (marks >= 75) {
print("Grade A");
} else if (marks >= 60) {
print("Grade B");
} else if (marks >= 40) {
print("Grade C");
} else {
print("Fail");
}
11. Logical Operators in Login Validation
String email = "[email protected]";
String password = "12345";
bool validEmail = email.isNotEmpty;
bool validPassword = password.length >= 5;
if (validEmail && validPassword) {
print("Login information is valid");
} else {
print("Invalid login information");
}
12. Conditional Operator in Flutter UI
Conditional expressions are useful when displaying different widgets or messages based on
application state.
bool isLoggedIn = true;
String buttonText = isLoggedIn
? "Logout"
: "Login";
print(buttonText);
A Flutter widget can also use a ternary expression:
Text(
isLoggedIn ? "Welcome Back!" : "Please Login",
)
13. Combining Different Operators
Multiple types of operators can be used together to solve real programming problems.
int age = 25;
double salary = 50000;
bool hasExperience = true;
if (age >= 18 && salary > 30000 && hasExperience) {
print("Eligible");
} else {
print("Not eligible");
}
14. Complete Dart Example
void main() {
int price = 1000;
int quantity = 2;
int discount = 200;
// Arithmetic
int subtotal = price * quantity;
// Assignment
int finalPrice = subtotal;
finalPrice -= discount;
// Comparison
bool isAffordable = finalPrice <= 2000;
// Logical
bool hasStock = true;
// Conditional
String message =
isAffordable && hasStock
? "Order can be placed"
: "Order cannot be placed";
print("Subtotal: ₹$subtotal");
print("Final Price: ₹$finalPrice");
print("Affordable: $isAffordable");
print(message);
}
15. Operator Precedence
When an expression contains multiple operators, Dart follows operator precedence rules
to determine the order in which operations are evaluated.
int result = 10 + 5 * 2;
print(result); // 20
Multiplication is evaluated before addition, so the expression behaves like:
int result = 10 + (5 * 2);
Parentheses can be used when you want to make the intended order explicit.
int result = (10 + 5) * 2;
print(result); // 30
16. Difference Between Comparison and Logical Operators
| Comparison Operators |
Logical Operators |
| Compare values |
Combine or reverse Boolean conditions |
==, !=, >, < |
&&, ||, ! |
| Usually produce a Boolean result |
Work with Boolean expressions |
age >= 18 |
age >= 18 && hasId |
17. Difference Between Assignment and Comparison
A common beginner mistake is confusing = with ==.
int age = 20; // Assignment
age == 20 // Comparison
= assigns a value.
== checks whether two values are equal.
18. Common Mistakes
Mistake 1: Using = Instead of ==
// Assignment
int age = 18;
// Comparison
if (age == 18) {
print("Age is 18");
}
Mistake 2: Confusing / and ~/
print(10 / 3); // 3.333333...
print(10 ~/ 3); // 3
Mistake 3: Forgetting Parentheses
int result = (10 + 5) * 2;
Mistake 4: Using && When || Is Required
Use && when all required conditions must be true. Use ||
when at least one condition can satisfy the requirement.
Mistake 5: Forgetting Null Values
String? username;
String name = username ?? "Guest";
19. Best Practices for Using Operators
- Use meaningful variable names with operators.
- Use parentheses when an expression could be difficult to read.
- Use comparison operators for clear Boolean conditions.
- Use logical operators to combine related conditions.
- Use the ternary operator for short and simple conditional expressions.
- Use
?? when a nullable value needs a safe fallback.
- Do not create unnecessarily complicated expressions.
- Keep business logic readable, especially in Flutter widgets.
20. Quick Revision Table
| Category |
Operators |
Example |
| Arithmetic |
+ - * / ~/ % |
10 + 5 |
| Assignment |
= += -= *= /= ~/= %= |
x += 5 |
| Comparison |
== != > < >= <= |
age >= 18 |
| Logical |
&& || ! |
age >= 18 && hasId |
| Ternary |
? : |
age >= 18 ? "Adult" : "Minor" |
| Null-aware |
?? ??= |
name ?? "Guest" |
21. Practice Questions
- Write a Dart program to add two numbers.
- Write a program to calculate the total price of three products.
- Use
% to determine whether a number is even or odd.
- Write a program using
+= and -=.
- Check whether a student has passed using comparison operators.
- Check whether a user is eligible based on age and another condition using
&&.
- Use
|| to allow access for either an admin or manager.
- Use
! to reverse a Boolean value.
- Use the ternary operator to display Pass or Fail.
- Use
?? to provide a default username.
22. Key Takeaways
- Arithmetic operators perform mathematical calculations.
- Assignment operators assign and update variable values.
- Comparison operators compare values and return Boolean results.
- Logical operators combine or reverse Boolean conditions.
- Conditional operators help select values based on conditions.
?? is useful for providing fallback values for nullable data.
- Operators are used extensively in Dart programs and Flutter application logic.
JustAcademy's current Flutter curriculum places operators within Dart Programming Fundamentals,
alongside variables, data types, control statements, functions, OOP, collections, and asynchronous
programming. :contentReference[oaicite:1]{index=1}
23. Learn Flutter with JustAcademy
If you want to continue learning Dart and Flutter through practical training, projects, API
integration, Firebase, UI development, testing, and deployment, explore the JustAcademy Flutter
training program. :contentReference[oaicite:2]{index=2}
Visit JustAcademy Flutter Training
Register for JustAcademy Course Demo