0% found this document useful (0 votes)
16 views5 pages

3 Operators in Dart

The document provides an overview of operators in Dart, including their types such as arithmetic, increment/decrement, assignment, relational, logical, and type test operators. It explains how each operator functions with examples and output for clarity. Understanding these operators is essential for performing mathematical and logical operations in Dart programming.

Uploaded by

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

3 Operators in Dart

The document provides an overview of operators in Dart, including their types such as arithmetic, increment/decrement, assignment, relational, logical, and type test operators. It explains how each operator functions with examples and output for clarity. Understanding these operators is essential for performing mathematical and logical operations in Dart programming.

Uploaded by

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

Operators In Dart

Operators are used to perform mathematical and logical operations on the


variables. Each operation in dart uses a symbol called the operator to denote the
type of operation it performs. Before learning operators in the dart, you must
understand the following things.

 Operands : It represents the data.


 Operator : It represents how the operands will be processed to produce a
value.

Types Of Operators

There are different types of operators in dart. They are as follows:

 Arithmetic Operators
 Increment and Decrement Operators
 Assignment Operators
 Logical Operators
 Type Test Operators

Arithmetic Operators

Arithmetic operators are the most common types of operators. They perform
operations like addition, subtraction, multiplication, division, etc.

Operator Symbol Operator Name Description


+ Addition For adding two operands
- Subtraction For subtracting two operands
For reversing the sign of the
-expr Unary Minus
expression
* Multiplication For multiplying two operands
For dividing two operands and
/ Division
give output in double
For dividing two operands and
~/ Integer Division
give output in integer
% Modulus Remainder After Integer Division

Example

void main() {
// declaring two numbers
int num1=10;
int num2=3;

// performing arithmetic calculation


int sum=num1+num2; // addition
int diff=num1-num2; // subtraction
int unaryMinus = -num1; // unary minus
int mul=num1*num2; // multiplication
double div=num1/num2; // division
int div2 =num1~/num2; // integer division
int mod=num1%num2; // show remainder

//Printing info
print("The addition is $sum.");
print("The subtraction is $diff.");
print("The unary minus is $unaryMinus.");
print("The multiplication is $mul.");
print("The division is $div.");
print("The integer division is $div2.");
print("The modulus is $mod.");
}

Output:

The addition is 13.


The subtraction is 7.
The unary minus is -10.
The multiplication is 30.
The division is 3.3333333333333335.
The integer division is 3.
The modulus is 1.

Increment and Decrement Operators

With increment and decrement operators, you can increase and decrease values.
If ++ is used at the beginning, then it is a prefix. If it is used at last, then it is
postfix.

Operator Symbol Operator Name Description


Increase Value By 1. var = var +
++var Pre Increment
1 Expression value is var+1
Decrease Value By 1. var = var -
--var Pre Decrement
1 Expression value is var-1
Increase Value By 1. var = var +
var++ Post Increment
1 Expression value is var
Decrease Value By 1. var = var -
var-- Post Decrement
1 Expression value is var
Example:

void main() {
// declaring two numbers
int num1=0;
int num2=0;

// performing increment / decrement operator

// pre increment
num2 = ++num1;
print("The value of num2 is $num2");

// reset value to 0
num1 = 0;
num2 = 0;

// post increment
num2 = num1++;
print("The value of num2 is $num2");

Output
The value of num2 is 1
The value of num2 is 0

Assignment Operators

It is used to assign some values to variables.

Operator Type Description


= Assign a value to a variable
+= Adds a value to a variable
-= Reduces a value to a variable
*= Multiply value to a variable
/= Divided value by a variable
void main() {
double age = 24;
age+= 1; // Here age+=1 means age = age + 1.
print("After Addition Age is $age");
age-= 1; //Here age-=1 means age = age - 1.
print("After Subtraction Age is $age");
age*= 2; //Here age*=2 means age = age * 2.
print("After Multiplication Age is $age");
age/= 2; //Here age/=2 means age = age / 2.
print("After Division Age is $age");
}
Output
After Addition Age is 25.0
After Aubtraction Age is 24.0
After Multiplication Age is 48.0
After Division Age is 24.0

Relational Operators

Relational operators are also called comparison operators. They are used to
make a comparison.

Operator Symbol Operator Name Description


Used to check which operand is
> Greater than
bigger and gives result as boolean
Used to check which operand is
< Less than
smaller and gives result as boolean
Used to check which operand is
>= Greater than or equal to bigger or equal and gives result as
boolean
Used to check which operand is
<= Less than or equal to smaller or equal and gives result as
boolean
Used to check operands are equal to
== Equal to each other and gives result as
boolean
Used to check operand are not equal
!= Not equal to to each other and gives result as
boolean

Example

void main() {

int num1=10;
int num2=5;
//printing info
print(num1==num2);
print(num1<num2);
print(num1>num2);
print(num1<=num2);
print(num1>=num2);
}

Logical Operators

It is used to compare values.

Operator Type Description


&& This is ‘and’, return true if all conditions are true
|| This is ‘or’. Return true if one of the conditions is true
This is ’not’. return false if the result is true and vice
!
versa

void main(){
int userid = 123;
int userpin = 456;

// Printing Info
print((userid == 123) && (userpin== 456)); // print true
print((userid == 1213) && (userpin== 456)); // print false.
print((userid == 123) || (userpin== 456)); // print true.
print((userid == 1213) || (userpin== 456)); // print true
print((userid == 123) != (userpin== 456));//print false

Type Test Operators

In Dart, type test operators are useful for checking types at runtime.

Operator Symbol Operator Name Description


Gives boolean value true if the
is is
object has a specific type
Gives boolean value false if the
is! is not
object has a specific type

void main() {
String value1 = "Dart Tutorial";
int age = 10;

print(value1 is String);
print(age is !int);
}-

You might also like