Dart OperatorsAn 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 OperatorsDart supports the following types of operators.
Dart Arithmetic OperatorsArithmetic 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 -
Example - Output: Example of Arithmetic operators n1+n2 = 15 n1-n2 = 5 n1*n2 = 50 n1/=n2 = 2 n1%n2 = 0
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.
Let's understand the following example - Example - Output: 30 26 9 12 Assignment OperatorAssignment 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.
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 OperatorRelational 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.
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 OperatorsThe Type Test Operators are used to testing the types of expressions at runtime. Consider the following table.
Let's understand the following example. Output: true false Dart Logical OperatorsThe Logical Operators are used to evaluate the expressions and make the decision. Dart supports the following logical operators.
Let's understand the following example. Output: Example of the logical operators false true false Dart Bitwise OperatorsThe 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.
Let's understand the following example - 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 OperatorsThe 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. Next TopicDart Constants |