Dart Operators
Dart Operators
In Dart, operators are special symbols or keywords that perform specific operations on
operands, such as variables, values, or expressions. They allow you to manipulate and
combine data in various ways. In this chapter, we will explore the Ddart operators, which
include three categories: prefix, infix, and suffix operators. Understanding these operators is
crucial for developing efficient and expressive code in Dart and Flutter.
1. Prefix Operators:
Prefix operators are placed before their operands and perform operations on the operand's
value. They are commonly used for incrementing or decrementing values, negating numbers,
or checking conditions. Let's take a closer look at some of the frequently used prefix
operators:
var count = 5;
count++; // Increment count by 1
print(count); // Output: 6
b) Negation (-)
The negation operator (-) is used to negate a numerical value. It changes a positive number
to negative and vice versa. For example:
2. Infix Operators:
Infix operators are placed between their operands and operate on two values
simultaneously. They are commonly used for arithmetic operations, comparison, and logical
operations. Let's explore some frequently used infix operators:
a) Arithmetic Operators
Arithmetic operators perform basic arithmetic operations like addition (+), subtraction (-),
multiplication (*), division (/), and remainder (%). For example:
var a = 10;
var b = 5;
var sum = a + b; // Addition
var difference = a - b; // Subtraction
var product = a * b; // Multiplication
var quotient = a / b; // Division
var remainder = a % b; // Remainder
b) Comparison Operators
Comparison operators compare two values and return a boolean value (true or false) based
on the comparison result. Common comparison operators include equal to == , not equal
to != , greater than > , less than < , greater than or equal to >= , and less than or equal to
<= . For example:
var x = 10;
var y = 5;
var isEqual = x == y; // Equal to
var isNotEqual = x != y; // Not equal to
var isGreaterThan = x > y; // Greater than
var isLessThan = x < y; // Less than
var isGreaterThanOrEqual = x >= y; // Greater than or equal to
var isLessThanOrEqual = x <= y; // Less than or equal to
c) Logical Operators
Logical operators combine boolean expressions and return a boolean value. The logical
operators include logical AND (&&), logical OR (||), and logical NOT (!). For example:
var a = true;
var b = false;
var result1 = a && b; // Logical AND
var result2 = a || b;
// Logical OR
var result3 = !a; // Logical NOT
3. Suffix Operators:
Suffix operators are placed after their operands and perform operations based on the
operand's value. They are mainly used for incrementing or decrementing values. Let's take a
look at the commonly used suffix operator:
var count = 5;
var originalValue = count++; // originalValue is assigned 5, and then count is incremented to 6
print(originalValue); // Output: 5
In this chapter, we covered the three categories of Ddart operators: prefix, infix, and suffix.
Understanding these operators will empower you to write concise and expressive code in
your Flutter applications. Practice using these operators in different scenarios to become
proficient in their usage.