📜  Dart的运算符

📅  最后修改于: 2021-09-02 05:51:36             🧑  作者: Mango

运算符符是用于对操作数执行某些操作的特殊符号。 Dart有大量的内置运算符,可用于执行不同的功能,例如,’+’ 用于将两个操作数相加。运算符旨在对一个或两个操作数进行操作。

Dart不同类型的运算符:

  1. 算术运算符
  2. 关系运算符
  3. 型式试验操作员
  4. 按位运算符
  5. 赋值运算符
  6. 逻辑运算符
  7. 条件运算符
  8. Casecade 符号运算符

1. 算术运算符:

这些运算符类包含用于对操作数执行算术运算的运算运算符。它们是二元运算符,即它们作用于两个操作数。他们是这样的:

Operator Symbol Operator Name Operator Description
+ Addition Use to add two operands
Subtraction Use to subtract two operands
-expr Unary Minus It is Use to reverse the sign of the expression
* Multiply Use to multiply two operands
/ Division Use to divide two operands
~/ Division Use two divide two operands but give output in integer
% Modulus Use to give remainder of two operands

示例:在程序中使用算术运算符

void main()
{
    int a = 2;
    int b = 3;
  
    // Adding a and b
    var c = a + b;
    print("Sum of a and b is $c");
  
    // Subtracting a and b
    var d = a - b;
    print("The difference between a and b is $d");
  
    // Using unary minus
    var e = -d;
    print("The negation of difference between a and b is $e");
  
    // Multipication of a and b
    var f = a * b;
    print("The product of a and b is $f");
  
    // Division of a and b
    var g = b / a;
    print("The quotient of a and b is $g");
  
    // Using ~/ to divide a and b
    var h = b ~ / a;
    print("The quotient of a and b is $h");
  
    // Remainder of a and b
    var i = b % a;
    print("The remainder of a and b is $i");
}

输出:

Sum of a and b is 5
The difference between a and b is -1
The negation of difference between a and b is 1
Product of a and b is 6
The quotient of a and b is 1.5
The quotient of a and b is 1
The remainder of a and b is 1

2. 关系运算符:

这些运算符类包含用于对操作数执行关系运算的运算运算符。他们是这样的:

Operator Symbol Operator Name Operator Description
> Greater than Check which operand is bigger and give result as boolean expression.
< Less than Check which operand is smaller and give result as boolean expression.
>= Greater than or equal to Check which operand is greater or equal to each other and give result as boolean expression.
<= less than equal to Check which operand is less than or equal to each other and give result as boolean expression.
== Equal to Check whether the operand are equal to each other or not and give result as boolean expression.
!= Not Equal to Check whether the operand are not equal to each other or not and give result as boolean expression.

示例:在程序中使用关系运算符

void main()
{
    int a = 2;
    int b = 3;
  
    // Greater between a and b
    var c = a > b;
    print("a is greater than b is $c");
  
    // Smaller between a and b
    var d = a < b;
    print("a is smaller than b is $d");
  
    // Greater than or equal to between a and b
    var e = a >= b;
    print("a is greater than b is $e");
  
    // Less than or equal to between a and b
    var f = a <= b;
    print("a is smaller than b is $f");
  
    // Equality between a and b
    var g = b == a;
    print("a and b are equal is $g");
  
    // Unequality between a and b
    var h = b != a;
    print("a and b are not equal is $h");
}

输出:

a is greater than b is false
a is smaller than b is true
a is greater than b is false
a is smaller than b is true
a and b are equal is false
a and b are not equal is true

3.型式试验操作员:

这些运算符类包含用于对操作数进行比较的那些运算符。他们是这样的:

Operator Symbol Operator Name Operator Description
is is Gives boolean value true as output if the object has specific type
is! is not Gives boolean value false as output if the object has specific type

示例:在程序中使用类型测试运算符

void main()
{
    String a = 'GFG';
    double b = 3.3;
  
    // Using is to compare
    print(a is String);
  
    // Using is! to compare
    print(b is !int);
}

输出:

true
true

4. 按位运算符:

这些运算符类包含用于对操作数执行按位运算的运算运算符。他们是这样的:

Operator Symbol Operator Name Operator Description
& Bitwise AND Performs bitwise and operation on two operands.
| Bitwise OR Performs bitwise or operation on two operands.
^ Bitwise XOR Performs bitwise XOR operation on two operands.
~ Bitwise NOT Performs bitwise NOT operation on two operands.
<< Left Shift Shifts a in binary representation to b bits to left and inserting 0 from right.
>> Right Shift Shifts a in binary representation to b bits to left and inserting 0 from left.

示例:在程序中使用位运算符

void main()
{
    int a = 5;
    int b = 7;
  
    // Performing Bitwise AND on a and b
    var c = a & b;
    print(c);
  
    // Performing Bitwise OR on a and b
    var d = a | b;
    print(d);
  
    // Performing Bitwise XOR on a and b
    var e = a ^ b;
    print(e);
  
    // Performing Bitwise NOT on a
    var f = ~a;
    print(f);
  
    // Performing left shift on a
    var g = a << b;
    print(g);
  
    // Performing right shift on a
    var h = a >> b;
    print(h);
}

输出:

5
7
2
4294967290
640
0

5.赋值运算符:

这些运算符类包含用于为操作数赋值的运算符。他们是这样的:

Operator Symbol Operator Name Operator Description
= Equal to Use to assign values to the expression or variable
??= Assignment opereator Assign the value only if it is null.

示例:在程序中使用赋值运算符

void main()
{
    int a = 5;
    int b = 7;
  
    // Assigning value to variable c
    var c = a * b;
    print(c);
  
    // Assigning value to variable d
    var d;
    d ? ? = a + b; // Value is assign as it is null
    print(d);
    // Again trying to assign value to d
    d ? ? = a - b; // Value is not assign as it is not null
    print(d);
}

输出:

35
12
12

6. 逻辑运算符:

这些运算符类包含用于逻辑组合操作数的两个或多个条件的那些运算符。他们是这样的:

Operator Symbol Operator Name Operator Description
&& And Operator Use to add two conditions and if both are true than it will return true.
|| Or Operator Use to add two conditions and if even one of them is true than it will return true.
! Not Operator It is use to reverse the result.

示例:在程序中使用逻辑运算符

void main()
{
    int a = 5;
    int b = 7;
  
    // Using And Operator
    bool c = a > 10 && b < 10;
    print(c);
  
    // Using Or Operator
    bool d = a > 10 || b < 10;
    print(d);
  
    // Using Not Operator
    bool e = !(a > 10);
    print(e);
}

输出:

false
true
true

7. 条件运算符:

这些运算符类包含用于对操作数进行比较的那些运算符。他们是这样的:

Operator Symbol Operator Name Operator Description
condition ? expersion1 : expersion2 Conditional Operator It is a simple version of if..else statement. If the condition is true than expersion1 is executed else expersion2 is executed.
expersion1 ?? expersion2 Conditional Operator If expersion1 is non-null returns its value else returns expression2 value.

示例:在程序中使用条件运算符

void main()
{
    int a = 5;
    int b = 7;
  
    // Conditional Statement
    var c = (a < 10) ? "Statement is Correct, Geek" : "Statement is Wrong, Geek";
    print(c);
  
    // Condtional statement
    int n;
    var d = n ? ? "n has Null value";
    print(d);
  
    // After assigning value to n
    n = 10;
    d = n ? ? "n has Null value";
    print(d);
}

输出:

Statement is Correct, Geek
n has Null value
10

8. Casecade 符号运算符:

这些运算符类允许您对同一元素执行一系列操作。它允许您对同一个对象执行多种方法。他们是这样的:

Operator Symbol Operator Name Operator Description
.. cascading Method It is used to perform multiple methods on the same object.

示例:在程序中使用 Casecade 符号运算符

class GFG {
    var a;
    var b;
  
    void set(x, y)
    {
        this.a = x;
        this.b = y;
    }
  
    void add()
    {
        var z = this.a + this.b;
        print(z);
    }
}
  
void main()
{
    // Creating objects of class GFG
    GFG geek1 = new GFG();
    GFG geek2 = new GFG();
  
    // Without using Cascade Notation
    geek1.set(1, 2);
    geek1.add();
  
    // Using Cascade Notation
    geek2..set(3, 4)
        ..add();
}

输出:

3
7