Introduction:
Explore the fundamental concepts of Dart operators and expressions with our comprehensive guide. Learn how to perform arithmetic operations, make logical decisions, and leverage assignment operators in Dart programming. This lesson provides hands-on examples and explanations to enhance your understanding of Dart’s powerful features.
In Dart, operators are symbols that represent computations, manipulations, or logical operations on variables and values. Expressions, on the other hand, are combinations of literals, variables, operators, and function invocations that can be evaluated to produce a value.
+ (addition)
– (subtraction)
* (multiplication)
/ (division)
% (modulo, returns the remainder of division)
dart
int a = 10; int b = 3; print(a + b); // 13 print(a - b); // 7 print(a * b); // 30 print(a / b); // 3.3333 print(a % b); // 1
== (equality)
!= (not equal)
> (greater than)
< (less than)
>= (greater than or equal to)
<= (less than or equal to)
Example:
dart
int x = 5; int y = 10; print(x == y); // false print(x != y); // true print(x > y); // false print(x < y); // true print(x >= y); // false print(x <= y); // true Logical Operators: && (logical AND) || (logical OR) ! (logical NOT)
dart
bool isTrue = true; bool isFalse = false; print(isTrue && isFalse); // false print(isTrue || isFalse); // true print(!isTrue); // false
= (assignment)
+= (add and assign)
-= (subtract and assign)
*= (multiply and assign)
/= (divide and assign)
Example:
dart
int num = 10; num += 5; // num is now 15 num -= 3; // num is now 12 num *= 2; // num is now 24 num /= 4; // num is now 6
dart
int a = 5; int b = 10; int result = (a > b) ? a : b; print(result); // 10
These are some of the basic operators in Dart. Understanding how to use them in expressions is essential for writing effective and concise Dart code.
Let’s create a complete Dart example that utilizes various operators and expressions to perform calculations and make logical decisions.
dart
void main() { // Arithmetic Operators int a = 10; int b = 3; int sum = a + b; // Addition int difference = a - b; // Subtraction int product = a * b; // Multiplication double quotient = a / b; // Division int remainder = a % b; // Modulo print("Arithmetic Operators:"); print("Sum: $sum"); print("Difference: $difference"); print("Product: $product"); print("Quotient: $quotient"); print("Remainder: $remainder"); print("\n"); // Relational Operators int x = 5; int y = 10; bool isEqual = x == y; bool isNotEqual = x != y; bool isGreaterThan = x > y; bool isLessThan = x < y; bool isGreaterOrEqual = x >= y; bool isLessOrEqual = x <= y; print("Relational Operators:"); print("Equal: $isEqual"); print("Not Equal: $isNotEqual"); print("Greater Than: $isGreaterThan"); print("Less Than: $isLessThan"); print("Greater or Equal: $isGreaterOrEqual"); print("Less or Equal: $isLessOrEqual"); print("\n"); // Logical Operators bool isTrue = true; bool isFalse = false; bool logicalAnd = isTrue && isFalse; bool logicalOr = isTrue || isFalse; bool logicalNot = !isTrue; print("Logical Operators:"); print("Logical AND: $logicalAnd"); print("Logical OR: $logicalOr"); print("Logical NOT: $logicalNot"); print("\n"); // Assignment Operators int num = 10; num += 5; // Add and assign num -= 3; // Subtract and assign num *= 2; // Multiply and assign num /= 4; // Divide and assign print("Assignment Operators:"); print("Final Value: $num"); print("\n"); // Conditional Operator (Ternary) int valueA = 5; int valueB = 10; int result = (valueA > valueB) ? valueA : valueB; print("Conditional Operator (Ternary):"); print("Result: $result"); }
The example begins by demonstrating arithmetic operations, relational comparisons, logical operations, and assignment operations.
It prints the results at each step, showing how the values change through different operations.
The last part demonstrates the conditional (ternary) operator, making a decision based on a condition.
You can run this Dart code to see the output and understand how these operators and expressions work together.
If you run the provided Dart example, the output should be as follows:
Arithmetic Operators:
Sum: 13
Difference: 7
Product: 30
Quotient: 3.3333333333333335
Remainder: 1
Relational Operators:
Equal: false
Not Equal: true
Greater Than: false
Less Than: true
Greater or Equal: false
Less or Equal: true
Logical Operators:
Logical AND: false
Logical OR: true
Logical NOT: false
Assignment Operators:
Final Value: 6
Conditional Operator (Ternary):
Result: 10
This output reflects the results of the various arithmetic, relational, logical, and assignment operations in the Dart example. The values are computed and printed at each step of the code.
Let’s create a simple Dart console application that incorporates the concepts of operators and expressions. In this example, we’ll create a calculator application that performs basic arithmetic operations based on input.
dart
import 'dart:io'; void main() { print("Welcome to the Simple Calculator!"); // Get input for two numbers double num1 = getNumber("Enter the first number: "); double num2 = getNumber("Enter the second number: "); // Perform arithmetic operations double sum = num1 + num2; double difference = num1 - num2; double product = num1 * num2; double quotient = num1 / num2; // Display results print("\nArithmetic Results:"); print("Sum: $sum"); print("Difference: $difference"); print("Product: $product"); print("Quotient: $quotient"); // Get input for a logical operation bool isTrue = getBoolean("Enter true or false: "); bool isFalse = !isTrue; // Perform logical operations bool logicalAnd = isTrue && isFalse; bool logicalOr = isTrue || isFalse; // Display logical results print("\nLogical Results:"); print("Logical AND: $logicalAnd"); print("Logical OR: $logicalOr"); } // Helper function to get a valid number from the double getNumber(String prompt) { double number; while (true) { try { stdout.write(prompt); String input = stdin.readLineSync()!; number = double.parse(input); break; } catch (e) { print("Invalid input. Please enter a valid number."); } } return number; } // Helper function to get a valid boolean from the bool getBoolean(String prompt) { bool value; while (true) { stdout.write(prompt); String input = stdin.readLineSync()!.toLowerCase(); if (input == 'true' || input == 'false') { value = input == 'true'; break; } else { print("Invalid input. Please enter 'true' or 'false'."); } } return value; }
Explanation:
The application starts by welcoming the to the Simple Calculator.
It then prompts the to enter two numbers for arithmetic operations and performs addition, subtraction, multiplication, and division.
The application also prompts the to enter a boolean value and performs logical AND and OR operations.
Helper functions getNumber and getBoolean are used to ensure that the input is valid.
The results of the calculations are displayed to the .
To run this Dart application, save it in a Dart file (e.g., calculator.dart) and execute it using the Dart SDK:
dart calculator.dart
Follow the prompts to enter numbers and boolean values, and the application will display the results based on the provided input.
Here’s a quiz with 15 questions about Dart operators and expressions. Each question is followed by an explanation of the correct answer.
a. Addition
b. Modulo
c. Multiplication
d. Division
a. &
b. &&
c. ||
d. !
a. 3
b. 3.33
c. 3.0
d. 4
a. Greater than or equal to
b. Not equal to
c. Less than or equal to
d. Equal to
a. =
b. ==
c. +=
d. /=
a. Logical AND
b. Logical OR
c. Logical NOT
d. Modulo
a. Greater than or equal to
b. Less than or equal to
c. Greater than
d. Less than
a. ||
b. &
c. !
d. &&
a. 15
b. 8
c. 18
d. 5
a. true
b. false
c. true && false
d. true || false
a. =
b. !=
c. ==
d. >=
a. Conditional (Ternary)
b. Null-aware coalescing
c. Logical AND
d. Logical OR
a. 2.5
b. 2
c. 3
d. 2.0
a. Decrement by 1
b. Increment by 1
c. Multiply by 2
d. Divide by 2
a. Assignment if null
b. Null-aware coalescing assignment
c. Logical OR assignment
d. Logical AND assignment
1-Answer: b. Modulo
The % operator in Dart is used for modulo, which returns the remainder of the division.
2-Answer: b. &&
The && operator is used for logical AND in Dart.
3-Answer: b. 3.33
The result of 10 / 3 in Dart is a floating-point number, approximately 3.33.
4-Answer: b. Not equal to
The != operator is used for “not equal to” comparison in Dart.
5-Answer: a. =
The = operator is used for assignment in Dart.
6-Answer: c. Logical NOT
The ! operator is used for logical NOT in Dart.
7-Answer: a. Greater than or equal to
The >= operator checks if the left operand is greater than or equal to the right operand.
8-Answer: a. ||
The || operator is used for logical OR in Dart.
9-Answer: a. 15
The num *= 3 operation multiplies num by 3. If num is initially 5, the result is 15.
10-Answer: c. true && false
(true && false) || true evaluates to false || true, which is true.
11-Answer: c. ==
The == operator is used for equality comparison in Dart.
12-Answer: b. Null-aware coalescing
The ?? operator is used for null-aware coalescing in Dart.
13-Answer: b. 2
The ~/ operator is the integer division operator in Dart, and 5 ~/ 2 results in 2.
14-Answer: b. Increment by 1
The ++ operator is used for incrementing a variable by 1.
15-Answer: b. Null-aware coalescing assignment
The ??= operator is used for null-aware coalescing assignment in Dart. It assigns the right-hand side to the left-hand side only if the left-hand side is null.
Simply want to say your article is as amazing. The clarity in your post is simply cool and i can assume you’re an expert on this subject. Well with your permission let me to grab your RSS feed to keep updated with forthcoming post. Thanks a million and please continue the rewarding work.
You really make it seem so easy together with your presentation however I find this matter to be really one thing which I believe I’d never understand. It sort of feels too complex and very broad for me. I’m looking forward to your next put up, I will attempt to get the hang of it!
I just could not go away your web site prior to suggesting that I really loved the usual information a person supply on your visitors? Is gonna be again regularly in order to check out new posts