Operator

Dart operators include arithmetic operators, relational operators, logical operators, and assignment operators.

1. Arithmetic operator (+ -,*,/,~/ (round),% (take over))

void main() {
  /* The arithmetic operator */
  int a = 10;
  int b = 3;

  print(a + b); / / add
  print(a - b); / /
  print(a * b); / / by
  print(a / b); / / in addition to
  print(a % b); / / the rest
  print(a ~/ b); / / integer
}
Copy the code

2. Relational operators (= =! =><> =< =)

/* Relational operator */
  int a = 4;
  int b = 6;

  print(a == b); // Check whether it is equal false
  print(a ! = b);// Check whether the difference is true
  print(a > b); // Check whether it is greater than false
  print(a < b); // Determine if the value is less than true
  print(a >= b); // Determine whether greater than or equal to false
  print(a <= b); // Check whether the value is less than or equal to true
Copy the code

3. Logical operators (!&&|| )

/* Logical operator */
  / /! (the)
  var bool = false;
  print(!bool); //true

  //&& (and) true if all true otherwise false
  var a = true;
  var b = true;
  print(a && b); //true

  / / | | (or) the value of false is false words, either to true value to true
  var c = true;
  var d = false;
  print(c || d); //true
Copy the code

4. Assignment operator (=,?? =,+ =- =,* =,/ =,~ / =,% =,++,--)

/* Assignment operator */
  / / =
  int a = 1;
  int b = 2;
  int c = a + b; // Calculate from right to left
  print(c); / / 3

  / /?? =
  vard; d ?? =5;
  print(d);

  //+= -= *= /= %= ~/=
  var e = 4.0;
  e += 5; // The same thing as e=e+5
  e -= 5; // The same thing as e=e-5
  e *= 5; // The same thing as e=e*5
  e /= 1; // The same thing as e=e/5
  //....
  
  // ++(increment), --(decrement)
  // If ++ is written first, then the operation is performed; if ++ is written first, then the operation is performed
  var f = 5;
  f++;
  print(f); / / 5 + 1 = 6

  f--;
  print(f); / / 6-1 = 5

  ++f;
  print(f); / / 6
  
  --f;
  print(f); / / 5

  var g = f++; // Evaluate first
  print(g); / / 5
  print(f); / / 6

  var h = ++f; // Evaluate first and then assign
  print(h); / / 6
  print(f); / / 6
Copy the code

Conditional expressions

1.if… else… And the switch… case…

/* Conditional expression */
  var age = 18;
  // if... else... The writing of
  if (age > 18) {
    print('of age');
  } else if (age == 18) {
    print('Coming of age');
  } else {
    print('minor');
  }
  
  //switch... case... The writing of
  switch (age) {
    case 19:
      print('of age');
      break;
    case 17:
      print('minor');
      break;
    default:
      print('Coming of age');
      break;
  }
Copy the code

2. Ternary operators

// Write the ternary operator
  print(age > 18? 'of age':age < 18? 'minor':'Coming of age');
Copy the code

3.?? The operator

/ /?? The way you write the operator
  var a = 1;
  var b = a ?? 2;
  print(b); // if a has a value, b=a, print 1

  var a;
  var b = a ?? 2;
  print(b); // if a has no value, b=2, print 2
Copy the code

Type conversion

1. Conversion between Number and String

  ToString () toString()
  int a = 10;
  var b = a.toString();
  print(b is String); //true

  Parse () int. Parse ()
  String c = '10.0';
  var d = int.parse(c);
  print(d is int); //true
  // If String c = '10.0' is a floating point number, it will cause an error.
  String c = '10';
  var d = double.parse(c);
  print(d is double); //true
Copy the code

2. Other types are converted to Booleans

// isEmpty: checks whether the string isEmpty
  var name = ' ';
  if (name.isEmpty) {
    print('name is empty');
  } else {
    print('Name not null');
  }
  
  var age = 18;
  if (age == 0) {
    print('0');
  } else {
    print(Not '0');
  }

  var num1;
  if (num1 == 0) {
    print('0');
  } else {
    print(Not '0');
  }

  var num2;
  if (num2 == null) {
    print('empty');
  } else {
    print('is not empty);
  }

  var num3 = 0 / 0;
  print(num3);
  if (num3.isNaN) {
    print('NaN');
  }
Copy the code

4. Loop statements

1. Three circulation methods

Take the sequence of 1-10 numbers printed as an example, the following three cycles:

main() {
  /* * a for loop* /
  for (var i = 1; i <= 10; i++) {
    print(i);
  }

  /* * the while loop* /
  int i = 1;
  while (i <= 10) {
    print(i);
    i++; // if i++ is not written, an infinite loop will occur
  }

  /** do while loop* /

  int i = 1;
  do {
    print(i);
    i++;
  } while (i <= 10);
}
Copy the code

2. Break and continue functions

Break statement function: 1, in the switch statement to make the flow out of the switch structure. 1. If a break statement has been executed in the loop, the statement after the break will not be executed. 2. In a multi-layer loop, a break statement can only break one layer. This can be used in the switch case as well as in the continue statement in the for and while loops: [Note] Can only be used in loop statements to end the loop, that is, to skip the statements under the loop body that have not yet been executed, and then to decide whether to execute the loop next time. Continue can be used in both for and while loops, but it is not recommended for while loops as they may die if you are not careful.Copy the code