dart Quick Start Tutorial (3)

3. Operator

On behalf of the operator is essentially a symbolic computation rules, such as: +, this symbol, which represents the math addition, operations can be carried out in accordance with the addition rule, empathy, learning operator is to have these rules only

3.1. Arithmetic Operators

Arithmetic operators including + + + ,, ,, ,, ~ * /,%

void main() {
  int a = 10;
  int b = 20;
  print(a + b);  // 30
  print(a - b);  // -10
  print(a * b); // 200
  print(a / b);  // 0.5
  print(a % b);  // 10
  print(211 ~/ 90); // 2
  a++;
  print(a);
  b--;
  print(b);
}

3.2. Logical Operators

Logical operators include:! , &&, ||

void main() {
  bool bl1 = true;
  bool bl2 = false;
  // 取反
  print(!bl1);
  // || 结果有真为真
  print(bl1 || bl2);
  // && 结果有假为假
  print(bl1 && bl2);
}

3.3. Assignment operator

Assignment operator comprising: a = ?? =, + =, - =, * =, / =

void main() {
  // 把10赋值给变量a
  int a = 10;
  int b = 20;
  // ??= 这个运算符规则:1. 如果b原来有值,那么就使用原来的值,这里原来的值为20
  b ??= 30;
  print(b);
  int c;
  //  2. 如果变量原来是空的,那么就把后面的值赋给这个变量,下面的代码c原来没有值,所以把40赋值给c
  c ??= 40;
  print(c);
}

Other rules operator is very simple, compound operators belonging to the following rules:

void main() {
  int a = 10;
  a += 20;  // 等价于 a = a + 20  
  print(a);  // 30
  // 规则以此类推  a -= 20  => a = a - 20 
  // a *= 20  => a = a * 20
  // a /= 20  => a = a / 20
  // ...
}

3.4. Comparison Operators

Comparison operator is mainly used to judge whether the two values ​​are equal, greater than, less than

void main() {
  int a = 10;
  int b = 20;
  print(a > b); // false
  print(a >= b); // false
  print(a < b);  // true
  print(a <= b); // true
  print(a == b);  //false
}

Note: print (a === b); write will complain

3.5. Conditional operator

1. The three head operation: condition? Expression 1: Expression 2

void main() {
  print(5 > 3 ? true: false);
}

2 ?? operator: Expression 1 Expression 2 ??

void main() {
  int a;
  int b = 20;
  int c = a ?? b;
  print(c);
}

Screw classroom video lessons Address: http://edu.nodeing.com

Guess you like

Origin www.cnblogs.com/dadifeihong/p/12047516.html
Recommended