In Dart, operators are special symbols that are used to perform operations on variables and values. Dart supports a variety of operators, including arithmetic, relational, logical, bitwise, assignment, and more. Understanding these operators is crucial for performing calculations, making decisions, and manipulating data in your programs.
Types of Operators
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Conditional Operators
- Type Test Operators
- Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
Operator | Description | Example |
---|---|---|
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Division | a / b |
% |
Modulus (remainder) | a % b |
++ |
Increment | a++ or ++a |
-- |
Decrement | a-- or --a |
Example:
void main() { int a = 10; int b = 5; print('a + b = ${a + b}'); // Addition print('a - b = ${a - b}'); // Subtraction print('a * b = ${a * b}'); // Multiplication print('a / b = ${a / b}'); // Division print('a % b = ${a % b}'); // Modulus a++; print('a++ = $a'); // Increment b--; print('b-- = $b'); // Decrement }
- Relational Operators
Relational operators are used to compare two values. They return a boolean value (true
or false
).
Operator | Description | Example |
---|---|---|
== |
Equal to | a == b |
!= |
Not equal to | a != b |
> |
Greater than | a > b |
< |
Less than | a < b |
>= |
Greater than or equal to | a >= b |
<= |
Less than or equal to | a <= b |
Example:
void main() { int a = 10; int b = 5; print('a == b: ${a == b}'); // Equal to print('a != b: ${a != b}'); // Not equal to print('a > b: ${a > b}'); // Greater than print('a < b: ${a < b}'); // Less than print('a >= b: ${a >= b}'); // Greater than or equal to print('a <= b: ${a <= b}'); // Less than or equal to }
- Logical Operators
Logical operators are used to combine multiple boolean expressions.
Operator | Description | Example |
---|---|---|
&& |
Logical AND | a && b |
` | ` | |
! |
Logical NOT | !a |
Example:
void main() { bool a = true; bool b = false; print('a && b: ${a && b}'); // Logical AND print('a || b: ${a || b}'); // Logical OR print('!a: ${!a}'); // Logical NOT }
- Bitwise Operators
Bitwise operators are used to perform bit-level operations on integer values.
Operator | Description | Example |
---|---|---|
& |
Bitwise AND | a & b |
` | ` | Bitwise OR |
^ |
Bitwise XOR | a ^ b |
~ |
Bitwise NOT | ~a |
<< |
Left shift | a << b |
>> |
Right shift | a >> b |
Example:
void main() { int a = 5; // 0101 in binary int b = 3; // 0011 in binary print('a & b: ${a & b}'); // Bitwise AND print('a | b: ${a | b}'); // Bitwise OR print('a ^ b: ${a ^ b}'); // Bitwise XOR print('~a: ${~a}'); // Bitwise NOT print('a << 1: ${a << 1}'); // Left shift print('a >> 1: ${a >> 1}'); // Right shift }
- Assignment Operators
Assignment operators are used to assign values to variables.
Operator | Description | Example |
---|---|---|
= |
Assign | a = b |
+= |
Add and assign | a += b |
-= |
Subtract and assign | a -= b |
*= |
Multiply and assign | a *= b |
/= |
Divide and assign | a /= b |
%= |
Modulus and assign | a %= b |
Example:
void main() { int a = 10; int b = 5; a += b; // a = a + b print('a += b: $a'); a -= b; // a = a - b print('a -= b: $a'); a *= b; // a = a * b print('a *= b: $a'); a /= b; // a = a / b print('a /= b: $a'); a %= b; // a = a % b print('a %= b: $a'); }
- Conditional Operators
Conditional operators are used to evaluate expressions based on a condition.
Operator | Description | Example |
---|---|---|
?: |
Ternary Operator | a ? b : c |
?? |
Null-aware Operator | a ?? b |
Example:
void main() { int a = 10; int b = 5; // Ternary Operator String result = a > b ? 'a is greater' : 'b is greater'; print(result); // Null-aware Operator int? c; int d = c ?? b; // If c is null, use b print(d); }
- Type Test Operators
Type test operators are used to check the type of an object.
Operator | Description | Example |
---|---|---|
is |
Type check | a is int |
is! |
Not type check | a is! int |
Example:
void main() { int a = 10; print('a is int: ${a is int}'); // Type check print('a is! String: ${a is! String}'); // Not type check }
Practical Exercises
Exercise 1: Arithmetic Operations
Write a Dart program that takes two numbers as input and performs all arithmetic operations on them.
Solution:
import 'dart:io'; void main() { print('Enter first number:'); int a = int.parse(stdin.readLineSync()!); print('Enter second number:'); int b = int.parse(stdin.readLineSync()!); print('a + b = ${a + b}'); print('a - b = ${a - b}'); print('a * b = ${a * b}'); print('a / b = ${a / b}'); print('a % b = ${a % b}'); }
Exercise 2: Relational and Logical Operations
Write a Dart program that takes two boolean values as input and performs all relational and logical operations on them.
Solution:
import 'dart:io'; void main() { print('Enter first boolean value (true/false):'); bool a = stdin.readLineSync()!.toLowerCase() == 'true'; print('Enter second boolean value (true/false):'); bool b = stdin.readLineSync()!.toLowerCase() == 'true'; print('a == b: ${a == b}'); print('a != b: ${a != b}'); print('a && b: ${a && b}'); print('a || b: ${a || b}'); print('!a: ${!a}'); }
Conclusion
In this section, we covered the various types of operators available in Dart, including arithmetic, relational, logical, bitwise, assignment, conditional, and type test operators. Understanding these operators is essential for performing calculations, making decisions, and manipulating data in your Dart programs. Make sure to practice the exercises provided to reinforce your understanding of these concepts. In the next section, we will delve into control flow statements, which will allow you to control the execution flow of your programs based on certain conditions.
Dart Programming Course
Module 1: Introduction to Dart
- Introduction to Dart
- Setting Up the Development Environment
- Your First Dart Program
- Basic Syntax and Structure