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

前端 未结 3 1258
日久生厌
日久生厌 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:18

    Which one is correct?

    Both: it is an expression statement. C and C++ let you put an expression into a body of code, add a semicolon, and make it a statement.

    Here are some more examples:

    x++;       // post-increment produces a value which you could use
    a = 5;     // Assignment produces a value
    max(a, b); // Call of a non-void function is an expression
    2 + x;     // This calculation has no side effects, but it is allowed
    

    Note that this is true in the specific case of C and C++, but may not be true in case of other languages. For example, the last expression statement from the list above would be considered invalid in Java or C#.

提交回复
热议问题