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

3

7

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 Expression Versus Statement
look at the following:

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

I could say that it is a statement as it is ending with a semi- colon BUT i could also say
It is an expression since i have an ostream , an output operator and a string literal
and this expression yields a value which is the left hand operand.
Which one is correct?

Scopoline answered 22/12, 2014 at 9:49 Comment(7)
<< is predefined operator for cout. Don't get it for example of statement and expression - it is more a demonstration for predefined operator in C++. Look printf function and normal expressions (with variables and constants).Minh
possible duplicate of Expression Versus StatementCongregation
@Poldie Sorry no, that's for c#Shun
@Minh Calling it "predefined" is misleading. It's provided by the library, not by the language.Selfcentered
@Poldie I told you read that answer>Scopoline
Well then every call to a method that returns something is an expression, following your way of thinking...Cleavers
@Poldie C# and C++ are different languages. Before you mark a C++ question as a duplicate of a C# one, it's presupposed that you are aware of the differences rather than having others fill in the blanks.Selfcentered
C
6

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#.

Cockleboat answered 22/12, 2014 at 9:56 Comment(0)
G
11

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.

Gautier answered 22/12, 2014 at 9:57 Comment(0)
C
6

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#.

Cockleboat answered 22/12, 2014 at 9:56 Comment(0)
A
2

The definition of expression is given in the C Standard (6.5 Expressions)

1 An expression is a sequence of operators and operands that specifies computation of a value, or that designates an object or a function, or that generates side effects, or that performs a combination thereof. The value computations of the operands of an operator are sequenced before the value computation of the result of the operator.

As for expression-statements then they are ended with a semicolon. Here is the definition of the expression statement in C++

expression-statement:
expression opt;

And

An expression statement with the expression missing is called a null statement.

Relative to the last quote I would like to point to a difference between C and C++. In C++ declarations are statements while in C declarations are not statements. So in C++ you may place a label before a declaration while in C you may not do so. So in C you have to place a null statement before a declaration. Compare

C++

Label:
int x;

C

Label: ;
int x;
Accretion answered 22/12, 2014 at 10:5 Comment(1)
The op has read the grammar and regurgitating it here is no help. Convoluting with C also not helpful.Boyar

© 2022 - 2024 — McMap. All rights reserved.