what's an expression and expression statement in c++?

前端 未结 7 1728
遇见更好的自我
遇见更好的自我 2020-12-04 12:42

I\'ve read that usually statements in c++ end with a semi-colon; so that might help explain what an expression statement would be. But then what would you call an expression

7条回答
  •  广开言路
    2020-12-04 13:09

    These are expressions (remember math?):

    1
    6 * 7
    a + b * 3
    sin(3) + 7
    a > b
    a ? 1 : 0
    func()
    mystring + gimmeAString() + std::string("\n")
    

    The following are all statements:

    int x;                            // Also a declaration.
    x = 0;                            // Also an assignment.
    if(expr) { /*...*/ }              // This is why it's called an "if-statement".
    for(expr; expr; expr) { /*...*/ } // For-loop.
    

    A statement is usually made up of an expression:

    if(a > b)           // a > b is an expr.
        while(true)     // true is an expr.
            func();     // func() is an expr.
    

提交回复
热议问题