Operators are special symbols which perform any arithmetic or logical operations. Let’s see below tables of available operators in dart language.

Let’s see these operators’ examples…

Increment/decrement operators [++ –]

In simple words, this operator basically increases/decreases values of variables.

Let’s see some coding examples…

Code 1:

void main(){
  int a=10;
  print(‘a is $a’);
  // increment operator ++
  a++;
  print(‘a is $a’);
 
  var b=20;
  print(‘b is $b’);
  b++;
  print(‘b is $b’);
  b++;
  b++;
  print(‘b is $b’);
}
output

Code 2: let’s see implementation of decrement operator…

void main(){
  int a=10;
  print(‘a is $a’);
  // decrement operator —
  a–;
  print(‘a is $a’);
 
  late var b;
  b=40;
  print(‘b is $b’);
  b–;
  b–;
  b–;
  print(‘b is $b’);
}
output

Let’s see another style of it…

Code 3:

void main(){
  int a=12;
  print(‘a is $a’);
  a++;
  print(‘a is $a’);
  // remember ++var var++ both works equal
  ++a;
  print(‘a is $a’);
 
  int b=20;
  print(‘b is $b’);
  b–;
  print(‘b is $b’);
  // remember var– –var both works equal
  –b;
  print(‘b is $b’);
}
output

Relational operators

Let’s see some examples of this operators [>=(greater than equal) <=(less than equal) >(greater than) <(less than) ]

Code 4:

void main(){
  int a=40;
  int b=20;
  if (a>b) {
    print(‘a is big!’);
  }
  else{
    print(‘b is big’);
  }
}
output

code 5:

void main(){
  int a=10;
  if (a>=8) {
    print(‘a is greater than 8’);
  }
  int b=20;
  if (b<=25) {
    print(‘b is less than 25’);
  }
}
output

Equality operators [== !=]

Code 6:

void main(){
  int a=10;
  int b=10;
  if (a==b) {
    print(‘a equlas b’);
  }
  String name=’TATA’;
  String name2=’TATA’;
  // equality operator ==
  if (name==name2) {
    print(‘both names same’);
  }
}
output

code 7:

void main(){
  int a=5;
  // Not equal !=
  if (a!=6) {
    print(‘a not equals to 6’);
  }
  String b=”Dart”;
  // Not equal !=
  if (b!=”dart”) {
    print(‘b is Dart not dart!’);
    print(‘condition matches!’);
  }
}
output

Logical operators [&&(AND) ||(OR) !(NOT)]

Let’s see usages of AND operator [1×0=0,0×0=0,1×1=1,0×0=0] … generally if both input becomes same then output same.

Code 8:

void main(){
  int a=10;
  int b=10;
  if (a==10 && b==10) {
    print(‘a and b both equal’);
  }
  String name=”abacus”;
  // AND operator &&
  if (name==”abacus” && name==”aba”) {
    print(‘strings matched!’);
  } else {
    print(‘strings not matched!’);
  }
}
output

Let’s see usages of OR operator [0+0=0,1+1=1,1+0=1,0+1=1] … generally at-least one input goes true then output goes true.

Code 9:

void main(){
  int a=10;
  if (a==12 || a==10) {
    print(‘one condition matched!’);
  }
  String name=’microcodes’;
  // OR operator ||
  if (name==’shimanta’ || name==’microcodes’) {
    print(‘string name matched!’);
  }
}
output

Assignment Operators

It treated as =    Let’s see some usages of this…

Code 10:

void main(){
  // we assigning value ’10’ into variable a
  int a=10;
  print(‘a is $a’);
  // now, we assigning value ’90’ into variable a
  a=90;
  print(‘a now $a’);
}
output

code 11:

void main(){
  // we assigning value ‘java’ into variable a
  var lang=”java”;
  print(‘language -> $lang’);
  // now, we assigning value ‘dart’ into variable a
  lang=”dart”;
  print(‘language now -> $lang’);
}
output

Let’s see some another assignment operators [ += *= -=]

a+=b means a=a+b

a*=b means a=a*b

a-=b means a=a-b

code 12:

void main(){
  int a=10,b=20;
  int val;
  val = a+=b;
  print(‘val is $val’);
  val = a-=b;
  print(‘val is $val’);
  val = a*=b;
  print(‘val is $val’);
}
output

Let’s see same use cases for double variables

Code 13:

void main(){
  double a=5.2,b=2.3;
  double val;
  val = a+=b;
  print(‘val is $val’);
  val = a-=b;
  print(‘val is $val’);
  val = a*=b;
  print(‘val is $val’);
}
output

Divide & modulus operators

Basically, divide a/b but returns double as output in Dart language. modulus a%b , it generally helps to store remainder.

Code 14:

void main(){
  int a=10;
  double b = a/2;
  print(b);
 
  var c=30;
  var d = c/2;
  print(d);
}
output

code 15:

void main(){
  int a=17;
  int b=3;
 
  print(‘quotient is’);
  print(a/b);
  print(‘remainder is’);
  print(a%b);
}
output

Conditional Operators

The conditional operator (? 🙂 is a ternary operator (it takes three operands). The conditional operator works as follows:

  • The first operand is implicitly converted to bool. It is evaluated and all side effects are completed before continuing.
  • If the first operand evaluates to true (1), the second operand is evaluated.
  • If the first operand evaluates to false (0), the third operand is evaluated.

The result of the conditional operator is the result of whichever operand is evaluated — the second or the third. Only one of the last two operands is evaluated in a conditional expression.

Syntax – expression 1 ? expression 2: expression 3

Let’s see usage of this…

Code 16:

void main(){
  int a=10;
  late int val;
  // a==10 true so, expression 2 => 3
  val = a==10?3:4;
  print(‘val is: $val’);
}
output

code 17:

void main(){
  var a=”microcodes”;
  var rs;
  rs=(a==”microcodes”?”shimanta”:”hacker”);
  print(‘rs is: $rs’);
}
output

code 18:

void main(){
  var a=23.4;
  var res;
  // a==23.3 -> false, so 2nd expression => 60.7
  res=(a==23.3?10.2:60.7);
  print(‘res is: $res’);
}
output

Remember: there have also arithmetic operators like addition (+), subtraction(-), multiplication(*), modulus(%). But we have done several operations with it above…