DEV Community

Daniel Ko
Daniel Ko

Posted on

Operations in Dart

    Today, we'll be taking a look at operators in the Dart language. A lot of the operators are things you'd expect from your Algebra class.

Arithmetic Operators

// addition
print(1 + 2); // will return 3

// subtraction
print(2 - 1); // will return 1

// multiplication
print(2 * 2); // will return 4

// double division
print(5 / 2); // will return 2.5;

// integer division
print(5 ~/ 2); // will return 2;

// modulus
print(5 %2 ); // will return 1;
Enter fullscreen mode Exit fullscreen mode
  • The difference between integer and double division is just the type of the return value.
  • Negative values are supported in these operators.
  • The output is entirely dependent on the operator so even if you did 5.0~/2.0, it would still return 2.
  • Modulus is the remainder from the division operation.

Increment / Decrement Operators

  • If you want a shorthand way to increase or decrease a value, you can use the ++ or -- operators.
  • There are two version of these as well, known as postfix / prefix.
    • Prefix: The value is incremented first, then returned.
    • Postfix: The value is returned, then incremented.
    • Let's take a look at an example to better understand this.
void main() {
  var a = 0;

  // what do you think the output will be?
  print(increaseNum(a));
}

int increaseNum(int num) {
  return num++;
}
Enter fullscreen mode Exit fullscreen mode
  • The output will be 0, because the function returns the number (which was 0) and then gets increased, but that doesn't matter for this example.
  • However, this example will produce the number we may have wanted.
void main() {
  var a = 0;

  // will print 1
  print(increaseNum(a));
}

int increaseNum(int num) {
  return ++num;
}
Enter fullscreen mode Exit fullscreen mode
  • There is another way to do a shorthand increment and assignment with += operator.
void main() {
  var a = 0;

  print(increaseNum(a));
}

int increaseNum(int num) {
  return num += 1;
}
Enter fullscreen mode Exit fullscreen mode
  • This will behave like a prefix increment.
  • In addition to that, you would have benefit of being able to increase by any amount, unlike the ++ operator which limits you to only increase of 1 (or decrease of 1 with --).

Assignment Operators

// basic assignment operator
var a = 1;

// will print 2
var a = 1;
print(a += 1);

// will print 0
var a = 1;
print(a -= 1);

// will print 1
var a = 1;
print(a *= 1);

// this actually won't compile because we can't assign a double to an int
var a = 1;
print(a /= 1);

// will print 1
var a = 1;
print(a ~/= 1);
Enter fullscreen mode Exit fullscreen mode
  • You might be wondering what exactly an assignment operator is. It is essentially just something that sets the value of what we have on the left side of the = to the value on the right side.

Relational Operators

// true
print(0 < 1);

// false
print(0 > 1);

// true
print(0 <= 1);

// false
print(0 >= 1);

// true
print(1 == 1);

// true
print(0 != 1);
Enter fullscreen mode Exit fullscreen mode
  • Relational operators evaluate two values on either side of it's operand and return either true or false depending on which it is.

Logical Operators

  • There's 3 logical operators:

    • !
      • This operator toggles the value to the opposite boolean value of what it currently is set to.
    • &&
      • This operator evaluates an expression on both sides and returns true if both sides are true, other wise returns false.
    • ||
      • This operator evaluates an expression on both sides and returns true if one side is true.
  • There are "truth tables" you can look up to get a better understanding of these logical operators. Here's an example of one:

Alt text of image

Conclusion

That's about it for operators in Dart. Up next is how to break up flow of logic in your programs utilizing Dart!
If you have any question, leave a comment or reach out to me directly and I'll do my best to help you and / or revise my post for better clarity.

Top comments (0)