Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 7, 2021 01:10 pm GMT

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

// additionprint(1 + 2); // will return 3// subtractionprint(2 - 1); // will return 1// multiplicationprint(2 * 2); // will return 4// double divisionprint(5 / 2); // will return 2.5;// integer divisionprint(5 ~/ 2); // will return 2;// modulusprint(5 %2 ); // will return 1;
  • 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++;}
  • 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;}
  • 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;}
  • 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 operatorvar a = 1;// will print 2var a = 1;print(a += 1);// will print 0var a = 1;print(a -= 1);// will print 1var a = 1;print(a *= 1);// this actually won't compile because we can't assign a double to an intvar a = 1;print(a /= 1);// will print 1var a = 1;print(a ~/= 1);
  • 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

// trueprint(0 < 1);// falseprint(0 > 1);// trueprint(0 <= 1);// falseprint(0 >= 1);// trueprint(1 == 1);// trueprint(0 != 1);
  • 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.


Original Link: https://dev.to/danko56666/dart-operators-369

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To