Back to quest
Control Flow & FunctionsLevel 15 / 32

Operators

Arithmetic, Logical, and Comparison operators

Operators are the symbols that allow you to perform calculations, compare values, and make decisions in your code.

1. Arithmetic Operators

Beyond basic math (+, -, *, /), Dart includes specific operators for integer division, modulo, and incrementing.

Math & Increment

example.dart
1int a = 10;
2int b = 3;
3 
4// Integer Division (Trims decimals)
5print(a ~/ b); // Output: 3
6 
7// Modulo (Remainder)
8print(a % b); // Output: 1
9 
10// Increment (++) and Decrement (--)
11int x = 0;
12x++; // x is now 1
13x--; // x is now 0
14 
15// Prefix vs Postfix
16int y = 5;
17print(y++); // Prints 5, THEN changes y to 6 (Postfix)
18print(++y); // Changes y to 7, THEN prints 7 (Prefix)

HintThe distinction between ++i (prefix) and i++ (postfix) is subtle but important in loop logic.

2. Type Test Operators (is, as)

Dart allows you to check types at runtime using is and is!, or cast types using as.

Checking Types

example.dart
1var value = 'Hello World';
2 
3if (value is String) {
4 print('It is a string with length ${value.length}');
5}
6 
7if (value is! int) {
8 print('It is definitely not a number');
9}
10 
11// Type Casting (Use carefully!)
12dynamic random = 'Text';
13String text = random as String; // Explicitly casting to String

HintRuntime type checking is crucial when dealing with dynamic data or API responses.

3. Assignment Operators

You can combine math and assignment (+=, -=) or use the powerful null-aware assignment (??=).

Compound Assignment

example.dart
1double score = 10.0;
2score += 5; // Same as: score = score + 5;
3score *= 2; // Same as: score = score * 2;
4 
5// Null-aware Assignment (??=)
6// Only assigns value if the variable is currently null
7String? name; // null by default
8name ??= 'Guest';
9print(name); // Guest
10 
11name ??= 'Alice';
12print(name); // Still Guest (was not null)

Hint??= is great for setting default values for variables that might be uninitialized.

4. Logical & Relational Operators

Used for boolean logic. Dart uses Short-Circuit Evaluation for && and ||.

Short-Circuit Logic

example.dart
1bool isGood = true;
2bool isCheap = false;
3 
4// AND (&&): Both must be true
5// If left side is false, it doesn't bother checking the right side.
6if (isGood && isCheap) {
7 print('Buy it!');
8}
9 
10// OR (||): One must be true
11// If left side is true, it skips checking the right side.
12if (isGood || isCheap) {
13 print('Consider it.');
14}

HintShort-circuiting improves performance and prevents errors (e.g., checking if an object is null before accessing a property).

5. Conditional Expressions

Dart provides concise ways to write if/else logic using ? : and ??.

Ternary & Null-Coalescing

example.dart
1int age = 20;
2 
3// Ternary Operator (condition ? ifTrue : ifFalse)
4String status = age >= 18 ? 'Adult' : 'Minor';
5 
6// Null-Coalescing Operator (??)
7// Returns the left expression if visible, otherwise the right
8String? userInput;
9String username = userInput ?? 'Anonymous';
10 
11print(username); // Anonymous

HintThe Ternary operator is cleaner than a 5-line if-else block for variable assignment. ?? is essential for handling nullable values safely.

0 / 5 cleared

Cleared0%
1

Practice Exercises

Test your understanding of Dart operators

1

Challenge 1

Easy

Write code that checks if a number is even using the modulo operator.

starter.dart
1int number = 42;
2// Your code here
2

Challenge 2

Medium

What is the key difference between `++x` and `x++`?

Your answer
3

Challenge 3

Easy

Use the null-aware operator (??) to provide a default value of "Guest" if name is null.

starter.dart
1String? name = null;
2String displayName = // Your code here
4

Challenge 4

Medium

Use the ternary operator to assign "Pass" if score >= 60, otherwise "Fail".

starter.dart
1int score = 75;
2String result = // Your code here
5

Challenge 5

Medium

What does short-circuit evaluation mean for the && operator?

Your answer

Loot collected

Key takeaways
  • Use ~/ for integer division.
  • % gives the remainder of a division.
  • == checks equality, != checks inequality.
  • && means AND, || means OR, ! means NOT.
Party chat

Comments