Explaining the difference between a statement and an expression in c++

前端 未结 3 1259
日久生厌
日久生厌 2021-01-12 09:39

I am trying to understand thoroughly the difference between a statement and an expression
But i am finding it confusing even after reading this answer

3条回答
  •  情深已故
    2021-01-12 10:33

    Let's see what the C++ grammar can tell us:

    statement:
      labeled-statement
      attribute-specifier-seq_opt expression-statement
      attribute-specifier-seq_opt compount-statement
      attribute-specifier-seq_opt selection-statement
      attribute-specifier-seq_opt iteration-statement
      attribute-specifier-seq_opt jump-statement
      declaration-statement
      attribute-specifier-seq_opt try-block
    
    expression-statement:
      expression_opt ';'
    

    So it is a statement; in particular, an "expression statement", which consists of a (potentially empty) expression followed by a semi-colon. In other words,

    std::cout << "Hello there? "
    

    is an expression, while

    std::cout << "Hello there? " ;
    

    is a statement.

提交回复
热议问题