0% found this document useful (0 votes)
13 views

Operators in Dart Language

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)
13 views

Operators in Dart Language

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/ 12

Operators in Dart

The operators are special symbols that are used to carry out certain operations
on the operands. The Dart has numerous built-in operators which can be used
to carry out different functions, for example, ‘+’ is used to add two operands.
Operators are meant to carry operations on one or two operands.
Different types of operators in Dart:
The following are the various types of operators in Dart:
1. Arithmetic Operators
2. Relational Operators
3. Type Test Operators
4. Bitwise Operators
5. Assignment Operators
6. Logical Operators
7. Conditional Operator
8. Cascade Notation Operator
1. Arithmetic Operators:
This class of operators contain those operators which are used to perform
arithmetic operation on the operands. They are binary operators i.e they act on
two operands. They go like this:

Operator Operator
Symbol Name Operator Description

+ Addition Use to add two operands

– Subtraction Use to subtract two operands

It is Use to reverse the sign of the


-expr Unary Minus
expression

* Multiply Use to multiply two operands

/ Division Use to divide two operands


Operator Operator
Symbol Name Operator Description

Use two divide two operands but give


~/ Division
output in integer

% Modulus Use to give remainder of two operands

Example: Using Arithmetic Operators in the program

• Dart

void main()
{
int a = 2;
int b = 3;

// Adding a and b
var c = a + b;
print("Sum of a and b is $c");

// Subtracting a and b
var d = a - b;
print("The difference between a and b is $d");

// Using unary minus


var e = -d;
print("The negation of difference between a and b is $e");

// Multiplication of a and b
var f = a * b;
print("The product of a and b is $f");

// Division of a and b
var g = b / a;
print("The quotient of a and b is $g");

// Using ~/ to divide a and b


var h = b ~ / a;
print("The quotient of a and b is $h");
// Remainder of a and b
var i = b % a;
print("The remainder of a and b is $i");
}

Output:
Sum of a and b is 5
The difference between a and b is -1
The negation of difference between a and b is 1
Product of a and b is 6
The quotient of a and b is 1.5
The quotient of a and b is 1
The remainder of a and b is 1
2. Relational Operators:
This class of operators contain those operators which are used to perform
relational operation on the operands. It goes like this:

Operator Operator
Symbol Name Operator Description

Check which operand is bigger and give result


> Greater than
as boolean expression.

Check which operand is smaller and give result


< Less than
as boolean expression.

Check which operand is greater or equal to


Greater than or
>= each other and give result as boolean
equal to
expression.

Check which operand is less than or equal to


less than equal
<= each other and give result as boolean
to
expression.

Check whether the operand are equal to each


== Equal to other or not and give result as boolean
expression.
Operator Operator
Symbol Name Operator Description

Check whether the operand are not equal to


!= Not Equal to each other or not and give result as boolean
expression.

Example: Using Relational Operators in the program

• Dart

void main()
{
int a = 2;
int b = 3;

// Greater between a and b


var c = a > b;
print("a is greater than b is $c");

// Smaller between a and b


var d = a < b;
print("a is smaller than b is $d");

// Greater than or equal to between a and b


var e = a >= b;
print("a is greater than b is $e");

// Less than or equal to between a and b


var f = a <= b;
print("a is smaller than b is $f");

// Equality between a and b


var g = b == a;
print("a and b are equal is $g");

// Unequality between a and b


var h = b != a;
print("a and b are not equal is $h");
}

Output:
a is greater than b is false
a is smaller than b is true
a is greater than b is false
a is smaller than b is true
a and b are equal is false
a and b are not equal is true
3. Type Test Operators:
This class of operators contain those operators which are used to perform
comparison on the operands. It goes like this:

Operator Operator
Symbol Name Operator Description

Gives boolean value true as output if the


is is
object has specific type

Gives boolean value false as output if the


is! is not
object has specific type

Example: Using Type Test Operators in the program

• Dart

void main()
{
String a = 'GFG';
double b = 3.3;

// Using is to compare
print(a is String);

// Using is! to compare


print(b is !int);
}

Output:
true
true
4. Bitwise Operators:
This class of operators contain those operators which are used to perform
bitwise operation on the operands. It goes like this:
Operator Operator
Symbol Name Operator Description

Performs bitwise and operation on two


& Bitwise AND
operands.

Performs bitwise or operation on two


| Bitwise OR
operands.

Performs bitwise XOR operation on two


^ Bitwise XOR
operands.

Performs bitwise NOT operation on two


~ Bitwise NOT
operands.

Shifts a in binary representation to b bits to left


<< Left Shift
and inserting 0 from right.

Shifts a in binary representation to b bits to left


>> Right Shift
and inserting 0 from left.

Example: Using Bitwise Operators in the program

• Dart

void main()
{
int a = 5;
int b = 7;

// Performing Bitwise AND on a and b


var c = a & b;
print(c);

// Performing Bitwise OR on a and b


var d = a | b;
print(d);
// Performing Bitwise XOR on a and b
var e = a ^ b;
print(e);

// Performing Bitwise NOT on a


var f = ~a;
print(f);

// Performing left shift on a


var g = a << b;
print(g);

// Performing right shift on a


var h = a >> b;
print(h);
}

Output:
5
7
2
4294967290
640
0
5. Assignment Operators:
This class of operators contain those operators which are used to assign value
to the operands. It goes like this:

Operator
Symbol Operator Name Operator Description

Use to assign values to the expression


= Equal to
or variable

Assignment
??= Assign the value only if it is null.
operator

Example: Using Assignment Operators in the program

• Dart
void main()
{
int a = 5;
int b = 7;

// Assigning value to variable c


var c = a * b;
print(c);

// Assigning value to variable d


var d;
d ? ? = a + b; // Value is assign as it is null
print(d);
// Again trying to assign value to d
d ? ? = a - b; // Value is not assign as it is not null
print(d);
}

Output:
35
12
12
6. Logical Operators:
This class of operators contain those operators which are used to logically
combine two or more conditions of the operands. It goes like this:

Operator Operator
Symbol Name Operator Description

Use to add two conditions and if both are true


&& And Operator
than it will return true.

Use to add two conditions and if even one of


|| Or Operator
them is true than it will return true.

! Not Operator It is use to reverse the result.

Example 1: Using Logical Operators in the program

• Dart
void main()
{
int a = 5;
int b = 7;

// Using And Operator


bool c = a > 10 && b < 10;
print(c);

// Using Or Operator
bool d = a > 10 || b < 10;
print(d);

// Using Not Operator


bool e = !(a > 10);
print(e);
}

Output:
false
true
true
Example 2: Logical operator can only be application to boolean expression
and in dart, non-zero numbers are not considered as true and zero as false:

• Dart

void main()
{
int a = 5;
int b = 7;

// Using And Operator


print(a && b);

// Using Or Operator
print(a || b);

// Using Not Operator


print(!a);
}
Output: Error: A value of type 'int' can't be assigned to a variable
of type 'bool'.
7. Conditional Operators:
This class of operators contain those operators which are used to perform
comparison on the operands. It goes like this:

Operator Operator
Symbol Name Operator Description

condition ? It is a simple version of if-else statement. If


Conditional
expersion1 : the condition is true than expersion1 is
Operator
expersion2 executed else expersion2 is executed.

expersion1 ?? Conditional If expersion1 is non-null returns its value


expersion2 Operator else returns expression2 value.

Example: Using Conditional Operators in the program

• Dart

void main()
{
int a = 5;
int b = 7;

// Conditional Statement
var c = (a < 10) ? "Statement is Correct, Geek" : "Statement is Wrong, Geek";
print(c);

// Conditional statement
int? n;
// Warning: Operand of null-aware operation '??' has type 'int' which excludes
null.
// For batter practice make both same type to avoid warning
// var d = n ?? 10;
var d = n ?? "n has Null value";
print(d);

// After assigning value to n


n = 10;
// we make it all ready null safe
//d = n ? ? "n has Null value";
d = n;
print(d);
}

Output:
Statement is Correct, Geek
n has Null value
10
Also In The Above Code,You May Notice That The Variable ‘n’ is Declared As “int?
n”.By declaring n as int?, you are indicating that the variable n can hold an integer
value or a null value. The ? denotes that the variable is nullable, meaning it can be
assigned a null value in addition to integer values.
8. Cascade Notation Operators:
This class of operators allows you to perform a sequence of operation on the
same element. It allows you to perform multiple methods on the same object.
It goes like this:

Operator Operator
Symbol Name Operator Description

cascading It is used to perform multiple methods on


..
Method the same object.

Example: Using Cascade Notation Operators in the program

• Dart

class GFG {
var a;
var b;

void set(x, y)
{
this.a = x;
this.b = y;
}

void add()
{
var z = this.a + this.b;
print(z);
}
}

void main()
{
// Creating objects of class GFG
GFG geek1 = new GFG();
GFG geek2 = new GFG();

// Without using Cascade Notation


geek1.set(1, 2);
geek1.add();

// Using Cascade Notation


geek2..set(3, 4)
..add();
}

Output:
3
7

You might also like