Dart Operators
Dart Operators
An operator is a symbol that is used to manipulating the values or performs operations on its operand. The given expression: 5+4, in this expression, 5 and 4 are operands and "+" is the operator.
Dart provides an extensive set of built-in operators to accomplish various types of operations. Operators can be unary or binary, which means unary take only on operand and binary take two operands with operators. There are several types of operators. Following is the list of Dart Operators.
Types of Operators
supports the following types of operators.- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Type test Operators
- Logical Operators
- Bitwise Operator
- Conditional Operators
- Casecade notation(..) Operators
Dart Arithmetic Operators
Arithmetic Operators are the most common operators that are used to perform addition, subtraction, multiplication, divide, etc. Let's take variable a holds 20 and variable b hold 10, then -
Sr. | Operator Name | Description | Example |
---|---|---|---|
1. | Addition(+) | It adds the left operand to the right operand. | a+b will return 30 |
2. | Subtraction(-) | It subtracts the right operand from the left operand. | a-b will return 10 |
3 | Divide(/) | It divides the first operand by the second operand and returns quotient. | a/b will return 2.0 |
4. | Multiplication(*) | It multiplies the one operand to another operand. | a*b will return 200 |
5. | Modulus(%) | It returns a reminder after dividing one operand to another. | a%b will return 0 |
6. | Division(~/) | It divides the first operand by the second operand and returns integer quotient. | a/b will return 2 |
7. | Unary Minus(-expr) | It is used with a single operand changes the sign of it. | -(a-b) will return -10 |
Example -
Output:
Example of Arithmetic operators n1+n2 = 15 n1-n2 = 5 n1*n2 = 50 n1/=n2 = 2 n1%n2 = 0
- Dart Unary Operators (post and pre)
In Java
, there are ++ and -- operators are known as increment and decrement operators and also known as unary operators, respectively. Unary operators, operate on single operand where ++ adds 1 to operands and -- subtract 1 to operand respectively.The unary operators can be used in two ways - postfix and prefix. If ++ is used as a postfix(like x++), it returns the value of operand first then increments the value of x. If -- is used as a prefix(like ++x), it increases the value of x.
Sr. | Operator Name | Description | Example |
---|---|---|---|
1. | ++(Prefix) | It increment the value of operand. | ++x |
2. | ++(Postfix) | It returns the actual value of operand before increment. | x++ |
3. | --(Prefix) | It decrement the value of the operand. | --x |
4. | --(Postfix) | It returns the actual value of operand before decrement. | x-- |
Let's understand the following example -
Example -
Output:
30 26 9 12
Assignment Operator
Assignment operators are used to assigning value to the variables. We can also use it combined with the arithmetic operators. The list of assignment operators is given below. Suppose a holds value 20 and b holds 10.
Operators | Name | Description |
---|---|---|
= (Assignment Operator) | It assigns the right expression to the left operand. | |
+=(Add and Assign) | It adds right operand value to the left operand and resultant assign back to the left operand. For example - a+=b → a = a+b → 30 | |
-=(Subtract and Assign) | It subtracts right operand value from left operand and resultant assign back to the left operand. For example - a-=b → a = a-b → 10 | |
*=(Multiply and Assign) | It multiplies the operands and resultant assign back to the left operand. For example - a*=b → a = a*b → 200 | |
/=(Divide and Assign) | It divides the left operand value by the right operand and resultant assign back to the left operand. For example - a%=b → a = a%b → 2.0 | |
~/=(Divide and Assign) | It divides the left operand value by the right operand and integer remainder quotient back to the left operand. For example - a%=b → a = a%b → 2 | |
%=(Mod and Assign) | It divides the left operand value by the right operand and remainder assign back to the left operand. For example - a%=b → a = a%b → 0 | |
<<=(Left shift AND assign) | The expression a<<=3 is equal to a = a<<3 | |
>>=(Right shift AND assign) | The expression a>>=3 is equal to a = a>>3 | |
&=(Bitwise AND assign) | The expression a&=3 is equal to a = a&3 | |
^=(Bitwise exclusive OR and assign) | The expression a^=3 is equal to a = a^3 | |
|=(Bitwise inclusive OR and assign) | The expression a|=3 is equal to a = a|3 |
Let's understand the following example -
Example -
Output:
Example of Assignment operators n1+=n2 = 15 n1-=n2 = 10 n1*=n2 = 50 n1~/=n2 = 10 n1%=n2 = 0
Relational Operator
Relational operators or Comparison operators are used to making a comparison between two expressions and operands. The comparison of two expressions returns the Boolean true and false. Suppose a holds 20 and b hold 10 then consider the following table.
Sr. | Operator | Description |
---|---|---|
1. | >(greater than) | a>b will return TRUE. |
2. | <(less than) | a<b will return FALSE. |
3. | >=(greater than or equal to) | a>=b will return TRUE. |
4. | <=(less than or equal to) | a<=b will return FALSE. |
5. | ==(is equal to) | a==b will return FALSE. |
6. | !=(not equal to) | a!=b will return TRUE. |
Let's understand the following example -
Example -
Output:
The example of Relational Operator a is greater than b: true a is less than b: false a is greater than or equal to b: true a is less than and equal to b: false a is not equal to b: true a is equal to b: false
Dart Type Test Operators
The Type Test Operators are used to testing the types of expressions at runtime. Consider the following table.
Sr. | Operator | Description |
---|---|---|
1. | as | It is used for typecast. |
2. | is | It returns TRUE if the object has specified type. |
3. | is! | It returns TRUE if the object has not specified type. |
Let's understand the following example.
Output:
true false
Dart Logical Operators
The Logical Operators are used to evaluate the expressions and make the decision. Dart supports the following logical operators.
Sr. | Operator | Description |
---|---|---|
1. | &&(Logical AND) | It returns if all expressions are true. |
2. | ||(Logical OR) | It returns TRUE if any expression is true. |
3. | !(Logical NOT) | It returns the complement of expression. |
Let's understand the following example.
Output:
Example of the logical operators false true false
Dart Bitwise Operators
The Bitwise operators perform operation bit by bit on the value of the two operands. Following is the table of bitwise operators.
Let's understand the following example.
Sr. | Operators | Description |
---|---|---|
1. | &(Binary AND) | It returns 1 if both bits are 1. |
2. | |(Binary OR) | It returns 1 if any of bit is 1. |
3. | ^(Binary XOR) | It returns 1 if both bits are different. |
4. | ~(Ones Compliment) | It returns the reverse of the bit. If bit is 0 then the compliment will be 1. |
5. | <<(Shift left) | The value of left operand moves left by the number of bits present in the right operand. |
6. | >>(Shift right) | The value of right operand moves left by the number of bits present in the left operand. |
Let's understand the following example -
Output:
Example of Bitwise operators a & b = 16 a | b = 29 a ^ b = 13 ~a = 4294967270 c<<1= 100 c>>1= 6
Dart Conditional Operators (?:)
The Conditional Operator is same as if-else statement and provides similar functionality as conditional statement. It is the second form of if-else statement. It is also identified as "Ternary Operator". The syntax is given below.
Syntax 1 -
If the given condition is TRUE then it returns exp1 otherwise exp2.
Syntax 2 -
If the exp1 is not-null, returns its value, otherwise returns the exp2's value.
Let's understand the following example.
Example - 1
Output:
20
Let's have a look at another scenario.
Example -2
Output:
value lesser than or equal to 30
Dart Cascade notation Operators
The Cascade notation Operators (..) is used to evaluate a series of operation on the same object. It is an identical as the method chaining that avoids several of steps, and we don't need store results in temporary variables
Comments
Post a Comment