• 【CPP】表达式


    1-if Statement

    if and if-else

    • Statements are exected conditionally
    # if.cpp
    #include 
    using namespace std;
    
    int main()
    {
        int num  = 10;
        if (num < 5)
            cout << "The number is less than 5. " << endl;
        
        if (num == 5 )
        {
            cout << "The number is 5." << endl;
        }
        else
        {
            cout << "The number is not 5." << endl;
        }
    
        if (num < 5)
            cout << "The number is less than 5." << endl;
        else if (num > 10)
            cout << "The number is greater than 10." << endl;
        else
            cout << "The number is in range [5, 10]." << endl;
    
        if(num < 20)
        if(num < 5)
        cout << "The number is less than 5" << endl;
        else
        cout << "Where I'm?" << endl;
    
        int * p = new int[1024];
        if (p)
            cout << "Memory has been allocated." << endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • When will “Where I’m?” be printed?
    • How to make the code easier to understand

    ? : operator

    • When can we use the ternary conditional operator?
    #ternary.cpp
    #include 
    using namespace std;
    
    int main()
    {
        bool isPositive = true;
        int factor = 0;
        //some operations may change isPositive's value
        if(isPositive)
            factor = 1;
        else
            factor = -1;
        //the if-else statement can be replaced by a ternary conditional operation
        factor = isPositive ? 1 : -1;
    
        //sometimes the following code can be more efficient.
        factor = isPositive * 2 - 1;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述

    2-Conditions

    Condition

    • What should be a condition
    int num = 10;
    if (num < 5)
       cout << "The number is less than 5." << endl;
    
    • 1
    • 2
    • 3
    • The condition should be an expression which is convertible to bool

    Its value can be bool, char, int, float

    Relational Expressions

    • The condition can be a relational expression
    • The 6 relational/comparison operators

    在这里插入图片描述

    • Return 1 if the condition (such as a == b) is true
    • Return 0 if the condition is false

    Logical Expressions

    • If an operands is not bool, it will be converted to bool implicitly

      在这里插入图片描述

    • Precedence: ! > && > ||

    • What’s the value of the follow expression?

    if (-2 && true)
      cout << "The condition is true." << endl;
    if (!-2)
      cout << "(!-2) is true, really?" << endl;
    
    • 1
    • 2
    • 3
    • 4

    Non-boolean Expressions

    • They will be converted to bool implicitly if it is feasible.
    float count = 0.2f;
    if (count).  // not recommend to use a float-point number
       cout << "There are some."  << endl;
       
    
    • 1
    • 2
    • 3
    • 4
    • Pointers are also frequently used as conditions
    int * p = new int[1024];
    if (!p) // if (p == NULL)
      cout << "Memory allocation failed." << ends;
    
    • 1
    • 2
    • 3

    3- While loop

    While loop

    -Syntax:

    While (expression)
    {
    }
    
    • 1
    • 2
    • 3
    • If the condition is true, the statement (loop body) will be executed
    #while.cpp
    #include 
    using namespace std;
    int main()
    {
        int num = 10;
        while(num > 0)
        {
            cout << "num = " << num << endl;
            num--;
        }
    
        // num = 10;
        // do
        // {
        //     cout << "num = " << num << endl;
        //     num--;
        // }while (num > 0);
    
        // num = 10;
        // while (num > 0)
        // {
        //     if (num == 5)
        //         break;
        //     cout << "num = " << num << endl;
        //     num--;
        // }
        return 0;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    在这里插入图片描述

    do-while loop

    • The test takes place after each iteration in a do-while loop
    • The test takes place before each iteration in a while loop
    #include 
    using namespace std;
    int main()
    {
        // int num = 10;
        // while(num > 0)
        // {
        //     cout << "num = " << num << endl;
        //     num--;
        // }
    
        int num = 10;
        do
        {
            cout << "num = " << num << endl;
            num--;
        }while (num > 0);
    
        // num = 10;
        // while (num > 0)
        // {
        //     if (num == 5)
        //         break;
        //     cout << "num = " << num << endl;
        //     num--;
        // }
        return 0;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    在这里插入图片描述

    break statement

    • Terminate a loop
    #include 
    using namespace std;
    int main()
    {
        // int num = 10;
        // while(num > 0)
        // {
        //     cout << "num = " << num << endl;
        //     num--;
        // }
    
        // int num = 10;
        // do
        // {
        //     cout << "num = " << num << endl;
        //     num--;
        // }while (num > 0);
    
        int num = 10;
        while (num > 0)
        {
            if (num == 5)
                break;
            cout << "num = " << num << endl;
            num--;
        }
        return 0;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    在这里插入图片描述

    continue statement

    • Skip the remaining part of the loop body and continue the next iteration
    #include 
    using namespace std;
    int main()
    {
        // int num = 10;
        // while(num > 0)
        // {
        //     cout << "num = " << num << endl;
        //     num--;
        // }
    
        // int num = 10;
        // do
        // {
        //     cout << "num = " << num << endl;
        //     num--;
        // }while (num > 0);
    
        // int num = 10;
        // while (num > 0)
        // {
        //     if (num == 5)
        //         break;
        //     cout << "num = " << num << endl;
        //     num--;
        // }
    
        int num = 10;
        while (num > 0)
        {
            if (num == 5)
                continue;
            cout << "num = " << num << endl;
            num--;
        }
        return 0;
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    死循环!

    The Condition, Be Careful

    • Can you find any problem from the code?
    size_t num = 10;
    while(num >= 0)
    {
      cout << "num = " << num << endl;
      num--;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    死循环!!

    size_t. 无符号, 减到0再减一则变成最大的数,没有负数,则循环停不下来

    bool flag = true;
    int count = 0;
    while (flag = true)
    {
      cout << "Count = " << count++ << endl;
      // and do sth
      if (count == 10)   // meet a condition
      flag = false;     // set flag to false to break the loop 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    while (flag = true)

    why?

    • Expression 3+4 has a value
    • Expression a+b has a value
    • Expression (a==b) has value (true or false)
    • a = b is an assignment, also an expression and has a value
    • The follow code can be compiled successfully
    int b = 0;
    int m = (b = 8);
    cout << "m=" << m << endl;
    
    
    • 1
    • 2
    • 3
    • 4

    4- for loop

    for loop

    • Syntax:

    for (init-clause; cons-expression; iteration-expression)
    loop-statement

    • Example
    #for.cpp
    #include 
    using namespace std;
    int main()
    {
        int sum = 0;
        for(int i = 0; i < 10; i++)
        {
            sum += i;
            cout << "Line " << i << endl;
        }
        cout << "sum = "  << sum << endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在这里插入图片描述

    for loop VS while loop

    在这里插入图片描述

    在这里插入图片描述

    Endless loop

    • Sometimes we need it
      在这里插入图片描述

    5-goto-switch

    goto statement

    • Jump to a desired location
    • An unrecommended statement
    #goto.cpp
    #include 
    
    using namespace std;
    
    float mysquare(float value)
    {
        float result = 0.0f;
    
        if(value >= 1.0f || value <= 0)
        {
            cerr << "The input is out of range." << endl;
            goto EXIT_ERROR;
        }
        result = value * value;
        return result;
    
      EXIT_ERROR:
        //do sth such as closing files here
        return 0.0f;
    }
    
    int main()
    {
        float value;
        cout << "Input a floating-point number." << endl;
        cin >> value;
    
        float result = mysquare(value);
    
        if (result > 0)
            cout << "The square is " << result << "." << endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    在这里插入图片描述

    switch statement

    • Execute one of several statements, depending on the value of a condition
    • break prevents to execute some following statements. Don’t forget break!
    • More similar with goto, not if-else
    #include 
    using namespace std;
    
    int main()
    {
        unsigned char input_char = 0;
    
        cout << "Please input a character to start." << endl;
        cin >> input_char;
        while (input_char != 'q')
        {
            switch (input_char)
            {
                case 'a':
                case 'A':
                    cout << "Move left. Input 'q' to quit." << endl;
                    break;
                case 'd':
                case 'D':
                    cout << "Move right. Input 'q' to quit." << endl;
                    break;
                default: 
                    cout << "Undefined key. Input 'q' to quit." << endl;
                    break;
            }
            cin >> input_char;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    在这里插入图片描述

  • 相关阅读:
    4、K8s控制器-Replicaset
    【Python 实战基础】Pandas对表格数据类型的创建与转换
    Prometheus学习
    14.1 Socket 套接字编程入门
    CTF—Php代码审计的ctf问题
    技术人必看!数据治理是什么?它对数据中台建设重要吗?
    路由传参的方式
    《最新出炉》系列初窥篇-Python+Playwright自动化测试-14-playwright操作iframe-番外篇
    sqli-labs/Less-59
    与想象不同的OPC UA publish/subscribe
  • 原文地址:https://blog.csdn.net/weixin_38362786/article/details/133900816