0% found this document useful (0 votes)
22 views3 pages

Dart Operators

Uploaded by

mesijis500
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views3 pages

Dart Operators

Uploaded by

mesijis500
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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:

a) Increment (++) and Decrement (--)


The increment and decrement operators increase or decrease the value of a variable by one,
respectively. For example:

var count = 5;
count++; // Increment count by 1
print(count); // Output: 6

var num = 10;


--num; // Decrement num by 1
print(num); // Output: 9

b) Negation (-)
The negation operator (-) is used to negate a numerical value. It changes a positive number
to negative and vice versa. For example:

var positiveNumber = 10;


var negativeNumber = -positiveNumber;
print(negativeNumber); // Output: -10

c) Logical Not (!)


The logical not operator (!) is used to invert the value of a boolean expression. It returns true
if the expression is false and false if the expression is true . For example:
var isLoggedIn = false;
var isLoggedOut = !isLoggedIn;
print(isLoggedOut); // Output: true

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:

a) Increment (++) and Decrement (--)


The increment and decrement operators (++ and --) are the same as the prefix operators, but
they have different effects when used as suffix operators. When used as suffix operators, they
return the original value before incrementing or decrementing. For example:

var count = 5;
var originalValue = count++; // originalValue is assigned 5, and then count is incremented to 6
print(originalValue); // Output: 5

var num = 10;


var previousValue = num--; // previousValue is assigned 10, and then num is decremented to
9
print(previousValue); // Output: 10

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.

You might also like