Dart Arithmetic Operators
Arithmetic operators in Dart are used to perform basic mathematical operations on numeric values. These operators allow you to manipulate integers and floating-point numbers easily. Here’s a detailed overview of the arithmetic operators available in Dart:
Arithmetic Operators in Dart
- Addition (
+
) - Subtraction (
-
) - Multiplication (
*
) - Division (
/
) - Integer Division (
~/
) - Modulus (
%
)
1. Addition (+
)
- Description: Adds two numbers together.
- Usage: Can be used with both
int
anddouble
types.
Example:
2. Subtraction (-
)
- Description: Subtracts the second number from the first.
- Usage: Can also be used with both
int
anddouble
types.
Example:
3. Multiplication (*
)
- Description: Multiplies two numbers.
- Usage: Applicable to both
int
anddouble
types.
Example:
4. Division (/
)
- Description: Divides the first number by the second. This operator always returns a
double
, even if both operands are integers. - Usage: Suitable for both
int
anddouble
types.
Example:
5. Integer Division (~/
)
- Description: Performs integer division, which divides and returns the quotient without the remainder. The result is an
int
. - Usage: Can be used with both
int
types.
Example:
6. Modulus (%
)
- Description: Returns the remainder of the division of the first number by the second.
- Usage: Applicable to both
int
anddouble
types.
Example:
Summary of Arithmetic Operators
Operator | Description | Example |
---|---|---|
+ | Addition | 5 + 3 (results in 8 ) |
- | Subtraction | 5 - 3 (results in 2 ) |
* | Multiplication | 5 * 3 (results in 15 ) |
/ | Division | 5 / 2 (results in 2.5 ) |
~/ | Integer Division | 5 ~/ 2 (results in 2 ) |
% | Modulus (Remainder) | 5 % 2 (results in 1 ) |
Conclusion
Arithmetic operators in Dart provide a straightforward way to perform mathematical calculations. Whether you're adding, subtracting, multiplying, or dividing, these operators are essential for manipulating numerical data in your Dart programs. Understanding how to use these operators effectively is crucial for developing robust applications and handling numeric computations.