|
void main() { |
|
|
|
// Operators are used to perform different kinds of operations on Data types. |
|
|
|
// 1.Arithmetic |
|
// These operators are used to perform calculations b/w 2 or more variables. |
|
// Example: Addition (+) |
|
int dartMarks; |
|
int javaScriptMarks; |
|
int cSharpMarks; |
|
|
|
dartMarks = 60; |
|
javaScriptMarks = 70; |
|
cSharpMarks = 85; |
|
print('Obtain marks: ${dartMarks + javaScriptMarks + cSharpMarks}'); |
|
|
|
// Example: Substraction (-) |
|
int totalBalance; |
|
int totalExpenses; |
|
|
|
totalBalance = 500; |
|
totalExpenses = 300; |
|
print('Remaining balance: ${totalBalance - totalExpenses}'); |
|
|
|
// Example: Multiplication (*) |
|
int square; |
|
square = 4; |
|
print('Square of $square is: ${square * square}'); |
|
|
|
// Example: Division (/) |
|
double totalWadges; |
|
totalWadges = 5048; |
|
print('There are 5 workers, each worker gets R.s: ${totalWadges / 5}'); |
|
|
|
// Example: Module (%) |
|
double value; |
|
value = 20; |
|
print('$value devided by 3, reminder is: ${value % 3}'); |
|
// End of 1.Arithmetic |
|
|
|
|
|
// 2.Arithmetic Assignment |
|
// These operators are used to perform calculations & also assign values to variables. |
|
// Example: Addition Assignment (+=) |
|
int a; |
|
a = 10; |
|
a += 5; // This statement same as a = a+5 |
|
print('a: $a'); |
|
|
|
// Example: Substraction Assignment (-=) |
|
int b; |
|
b = 10; |
|
b -= 5; |
|
print('b: $b'); |
|
|
|
// Example: Multiplication Assignment (*=) |
|
int c; |
|
c = 10; |
|
c *= 5; |
|
print('c: $c'); |
|
|
|
// Example: Division Assignment (/=) |
|
double d; |
|
d = 10; |
|
d /= 5; |
|
print('d: $d'); |
|
|
|
// Example: Module Assignment (%=) |
|
int e; |
|
e = 10; |
|
e %= 5; |
|
print('e: $e'); |
|
// End of 2.Arithmetic Assignment |
|
|
|
|
|
// 3.Relational |
|
// These operators are used to perform comparison b/w 2 operands & return result in bool (true/false). |
|
// Examples: |
|
int age; |
|
int otherAge; |
|
|
|
age = 17; |
|
otherAge = 18; |
|
|
|
// Example: Greater than (>) |
|
bool greaterThan; |
|
greaterThan = (age > otherAge); // This (age > otherAge) is called a condition. |
|
print('Greater than (>): $greaterThan'); |
|
|
|
// Example: Less than (<) |
|
bool lessThan; |
|
lessThan = (age < otherAge); |
|
print('Less than (<): $lessThan'); |
|
|
|
// Example: Greater than or equal to (>=) |
|
bool greaterThanOrEqualTo; |
|
greaterThanOrEqualTo = (age >= otherAge); |
|
print('Greater than or equal to (>=): $greaterThanOrEqualTo'); |
|
|
|
// Example: Less than or equal to: (<=) |
|
bool lessThanOrEqualTo; |
|
lessThanOrEqualTo = (age <= otherAge); |
|
print('Less than or equal to: (<=): $lessThanOrEqualTo'); |
|
|
|
// Example: Equal to: (==) |
|
bool equalTo; |
|
equalTo = (age == otherAge); |
|
print('Equal to: (==): $equalTo'); |
|
|
|
// Example: Not equal to: (!=) |
|
bool notEqualTo; |
|
notEqualTo = (age != otherAge); |
|
print('Not equal to: (!=): $notEqualTo'); |
|
// End of 3.Relational |
|
|
|
|
|
// 4.Logical |
|
// These operators are used to perform comparison b/w 2 or more conditions & return result in bool (true/false). |
|
// Examples: |
|
int numOne; |
|
int numTwo; |
|
String color; |
|
|
|
numOne = 50; |
|
numTwo = 60; |
|
color = 'red'; |
|
|
|
// Example: Logical OR (||): |
|
// Returns true if any condition is true. |
|
bool logicalOr; |
|
logicalOr = numOne == numTwo || color == 'red'; |
|
print('Logical OR (||): $logicalOr'); |
|
|
|
// Example: Logical AND (&&): |
|
// Returns true if all conditions are true. |
|
bool logicalAnd; |
|
logicalAnd = numOne == numTwo && color == 'red'; |
|
print('Logical AND (&&): $logicalAnd'); |
|
|
|
// Example: Logical NOT (!) with a condition: |
|
// Returns inverse of the conditions result. |
|
bool logicalNot; |
|
|
|
logicalNot = (!(numOne == numTwo) && color == 'red'); |
|
print('Logical NOT (!): $logicalNot'); |
|
|
|
// Example: Logical NOT (!) with Logical OR (||): |
|
// Returns inverse of the Logical OR (||) result. |
|
logicalNot = (!(numOne == numTwo || color == 'red')); |
|
print('Logical NOT (!): $logicalNot'); |
|
|
|
// Example: Mix Logical OR (||) AND (&&): |
|
bool mixLogicalOrAnd; |
|
mixLogicalOrAnd = (numOne == numTwo || color == 'red') && (numOne == numTwo && color == 'red'); |
|
print('(Logical OR (||)) && (Logical AND (&&)): $mixLogicalOrAnd'); |
|
// End of 4.Logical |
|
} |