[Flutter Development Practice] Dart Basics: Common Operators

Insert image description here

In Dart, operators are one of the basic building blocks for writing any program. This article will introduce the commonly used operators in Dart in detail to help beginners better understand and apply these concepts.

1. Arithmetic operators

Arithmetic operators are used to perform basic mathematical operations. Dart supports common addition, subtraction, multiplication, division, integer division, and remainder operations. Common arithmetic operators are shown in the following table:

operator describe Example
+ addition a + b
- Subtraction a - b
* multiplication a * b
/ division a / b
% Take remainder a % b
~/ Divisible a ~/ b
void main() {
    
    
  int a = 10;
  int b = 3;

  // 加法
  print("加法: ${
      
      a + b}");

  // 减法
  print("减法: ${
      
      a - b}");

  // 乘法
  print("乘法: ${
      
      a * b}");

  // 除法
  print("除法: ${
      
      a / b}");
  
  // 整除
  print("除法: ${
      
      a ~/ b}");

  // 取余
  print("取余: ${
      
      a % b}");
}

2. Relational operators

Relational operators are used to compare the relationship between two values ​​and return a Boolean value (true or false). In Dart, common relational operators are:

operator describe Example
== equal x == y
!= not equal to x != y
< less than x < y
> more than the x > y
<= less than or equal to x <= y
>= greater or equal to x >= y
void main() {
    
    
  int x = 5;
  int y = 8;

  // 相等
  print("相等: ${
      
      x == y}");

  // 不等
  print("不等: ${
      
      x != y}");

  // 大于
  print("大于: ${
      
      x > y}");

  // 小于
  print("小于: ${
      
      x < y}");

  // 大于等于
  print("大于等于: ${
      
      x >= y}");

  // 小于等于
  print("小于等于: ${
      
      x <= y}");
}

3. Logical operators

Logical operators are used to combine multiple conditions and are often used in control processes and judgment statements. Common logical operators are:

operator describe Example
&& logical AND isTrue && isFalse
|| logical or isTrue || isFalse
! logical negation !isTrue
void main() {
    
    
  bool isTrue = true;
  bool isFalse = false;

  // 与运算
  print("与运算: ${
      
      isTrue && isFalse}");

  // 或运算
  print("或运算: ${
      
      isTrue || isFalse}");

  // 非运算
  print("非运算: ${
      
      !isTrue}");
}

4. Assignment operator

The assignment operator is used to assign a value to a variable. There are simple assignment operators in Dart =, as well as some compound assignment operators, such as +=, -=, *=, /=etc., which are used to simplify assignment statements.

operator describe Example
= Assignment a = b
+= Assign after addition a += b
-= assignment after subtraction a -= b
*= Assign after multiplying a *= b
/= assignment after division a /= b
%= Assign value after taking remainder a %= b
~/= Assign value after division a ~/= b
void main() {
    
    
  int a = 5;
  int b = 10;

  a += b;
  print("a += b: $a");

  a -= b;
  print("a -= b: $a");

  a *= b;
  print("a *= b: $a");

  a /= b;
  print("a /= b: $a");
  
  a %= b;
  print("a /= b: $a");
}


Dart provides a rich set of operators, covering various needs from basic mathematical operations to logical judgments. Being proficient in these operators is an important step in becoming a good Dart programmer. But Dart’s operators are far more than those introduced in this article, but the operators introduced in this article can already meet most of the needs of subsequent development; if you want to know more operators of Dart, you can visit the link below to view the official document:

I hope this article has helped beginners understand Dart operators, and also encourages everyone to continue to explore and apply this knowledge in practice.

Author information

Author: Fanyi
CSDN: https://techfanyi.blog.csdn.net
Nuggets: https://juejin.cn/user/4154386571867191

Supongo que te gusta

Origin blog.csdn.net/qq_21484461/article/details/135468726
Recomendado
Clasificación