第 5 節

operator

0瀏覽次數0訪問次數--跳出率--平均停留

operator

Function: For performing code computations

In this chapter, we will mainly cover the following types of operators:

Operator TypesPurpose
arithmetic operatorsfor processing four arithmetic operations
Assignment operatorAssigns the value of an expression to a variable
Comparison OperatorsUsed for comparing expressions and returning a true or false value.
Logical operatorsUsed to return a true or false value based on the result of an expression.

arithmetic operators

Purpose: Used to handle arithmetic operations

Arithmetic operators include the following symbols:

OperatorTerminologyExampleResults
+Plus sign+33
-minus sign-3-3
+add10 + 515
-subtract10 - 55
*multiply10 * 550
/remove10 / 52
%modulo (remainder)10 % 31
++pre-incrementa=2; b=++a;a=3; b=3;
++Post-incrementa=2; b=a++;a=3; b=2;
--pre-decrementa=2; b=--a;a=1; b=1;
--post-decrementa=2; b=a--;a=1; b=2;

Example 1:

//加减乘除
int main() {

    int a1 = 10;
    int b1 = 3;

    cout << a1 + b1 << endl;
    cout << a1 - b1 << endl;
    cout << a1 * b1 << endl;
    cout << a1 / b1 << endl;  //两个整数相除结果依然是整数

    int a2 = 10;
    int b2 = 20;
    cout << a2 / b2 << endl; 

    int a3 = 10;
    int b3 = 0;
    //cout << a3 / b3 << endl; //报错,除数不可以为0

    //两个小数可以相除
    double d1 = 0.5;
    double d2 = 0.25;
    cout << d1 / d2 << endl;


    return 0;
}

运行结果:

13
7
30
3
0
2

Summary: In division operations, the divisor cannot be zero.

Example 2:

//取模
int main() {

    int a1 = 10;
    int b1 = 3;

    cout << 10 % 3 << endl;

    int a2 = 10;
    int b2 = 20;

    cout << a2 % b2 << endl;

    int a3 = 10;
    int b3 = 0;

    //cout << a3 % b3 << endl; //取模运算时,除数也不能为0

    //两个小数不可以取模
    double d1 = 3.14;
    double d2 = 1.1;

    //cout << d1 % d2 << endl;


    return 0;
}

运行结果:

1
10

Summary: Only integer variables can perform modulo operations.

Example 3:

//递增
int main() {

    //后置递增
    int a = 10;
    a++; //等价于a = a + 1
    cout << a << endl; // 11

    //前置递增
    int b = 10;
    ++b;
    cout << b << endl; // 11

    //区别
    //前置递增先对变量进行++,再计算表达式
    int a2 = 10;
    int b2 = ++a2 * 10;
    cout << b2 << endl;

    //后置递增先计算表达式,后对变量进行++
    int a3 = 10;
    int b3 = a3++ * 10;
    cout << b3 << endl;


    return 0;
}

运行结果:

11
11
110
100

Summary: Prefix increment applies ++ to the variable first before evaluating the expression, while postfix increment does the opposite.

Assignment operator

Function: Used to assign the value of an expression to a variable

赋值运算符包括以下几个符号:=、+=、-=、*=、/=、%=。

OperatorTerminologyExampleResults
=assign valuea=2; b=3;a=2; b=3;
+=plus equalsa=0; a+=2;a=2;
-=minus equalsa=5; a-=3;a=2;
*=Multiply equalsa=2; a*=2;a=4;
/=division equalsa=4; a/=2;a=2;
%=Modulo equalsa=3; a%2;a=1;

Example:

int main() {

    //赋值运算符

    // =
    int a = 10;
    a = 100;
    cout << "a = " << a << endl;

    // +=
    a = 10;
    a += 2; // a = a + 2;
    cout << "a = " << a << endl;

    // -=
    a = 10;
    a -= 2; // a = a - 2
    cout << "a = " << a << endl;

    // *=
    a = 10;
    a *= 2; // a = a * 2
    cout << "a = " << a << endl;

    // /=
    a = 10;
    a /= 2;  // a = a / 2;
    cout << "a = " << a << endl;

    // %=
    a = 10;
    a %= 2;  // a = a % 2;
    cout << "a = " << a << endl;


    return 0;
}

运行结果:

a = 100
a = 12
a = 8
a = 20
a = 5
a = 0

Comparison Operators

Purpose: Used for comparing expressions and returning a true or false value.

Comparison operators use the following symbols:

OperatorTerminologyExampleResults
==equivalent to4 == 30
!=not equal to4 != 31
<less than4 < 30
>greater than4 > 31
<=less than or equal to4 <= 30
>=greater than or equal to4 >= 11

Example:

int main() {

    int a = 10;
    int b = 20;

    cout << (a == b) << endl; // 0 

    cout << (a != b) << endl; // 1

    cout << (a > b) << endl; // 0

    cout << (a < b) << endl; // 1

    cout << (a >= b) << endl; // 0

    cout << (a <= b) << endl; // 1
    

    return 0;
}

运行结果:

0
1
0
1
0
1

Note: In comparison operations for C and C++ languages, "true" is represented by the number "1," and "false" is represented by the number "0."

Logical operators

Purpose: Returns true or false based on the value of an expression.

Logical operators have the following symbols:

OperatorTerminologyExampleResults
!Non-!aIf a is false, then !a is true; if a is true, then !a is false.
&&anda && bIf both a and b are true, the result is true; otherwise, it is false.
||ora ||bIf either a or b is true, the result is true; if both are false, the result is false.

Example 1: Logical NOT

//逻辑运算符  --- 非
int main() {

    int a = 10;

    cout << !a << endl; // 0

    cout << !!a << endl; // 1


    return 0;
}

运行结果:

0
1

Summary: What is real becomes fake, and what is fake becomes real.

Example 2: Logic AND

//逻辑运算符  --- 与
int main() {

    int a = 10;
    int b = 10;

    cout << (a && b) << endl;// 1

    a = 10;
    b = 0;

    cout << (a && b) << endl;// 0 

    a = 0;
    b = 0;

    cout << (a && b) << endl;// 0


    return 0;
}

运行结果:

1
0
0

Summary: Summary of the logical ==AND== operator: True only when both sides are true, otherwise false.

Example 3: logical OR

//逻辑运算符  --- 或
int main() {

    int a = 10;
    int b = 10;

    cout << (a || b) << endl;// 1

    a = 10;
    b = 0;

    cout << (a || b) << endl;// 1 

    a = 0;
    b = 0;

    cout << (a || b) << endl;// 0


    return 0;
}

运行结果:

1
1
0

Logical ==OR== operator summary: ==False only when both are false, true otherwise==

音乐页