4

I heard that if an expression is followed by a semicolon, then it is considered to be an expression statement.

Source: http://farside.ph.utexas.edu/teaching/329/lectures/node11.html

int x = 7;
x = 8;
x++;
x—-;
x = x << 1;

These are all expression statements.

But is this an expression statement too?

return 5;

And if not, then please throughly explain why.

And I would also appreciate it if you could tell whether the return satetement can be considered an expression statement in other languages as well.

Patrik Nusszer
  • 460
  • 3
  • 13
  • No, and what difference does it make? – 500 - Internal Server Error Jul 18 '19 at 14:10
  • @500-internalservererror Because if it is not an expression statement then the above definition for it is false and I failed to get the idea of what really makes an expression statement, which I would like to know. – Patrik Nusszer Jul 18 '19 at 14:13
  • Every herring is a fish, but not every fish is a herring. Every expression statement is an expression followed by a semicolon, but not every expression followed by a semicolon is an expression statement. – n. m. could be an AI Jul 18 '19 at 14:15
  • @n.m.: Not every expression statement is an expression followed by a semicolon. An expression statement does not need to have an expression. – Eric Postpischil Jul 18 '19 at 14:19
  • Which definition is wrong? Or which part of it? The linked page does not mention `return` at all. – Gerhardh Jul 18 '19 at 14:31
  • @Gerhardh the linked page only says that an expression followed by a semicolon is an expression statement, and does not mention the return statement. But a return statement also includes an expression terminated by a semicolon hence my confusion, but turns out the definition only holds true if the statement is comprised by nothing but an expression and a semicolon. In fact, the definition is true, but still made me confused anyway. – Patrik Nusszer Jul 18 '19 at 14:34
  • It says: An expression statement has an expression and a semicolon. It does not say anything in the opposite direction. A => B does not imply B => A. – Gerhardh Jul 18 '19 at 14:37
  • @Gerhardh yes, the root of the problem is merely my misinterpretation of the given definition and overlooking. It says it consists of an expression and a semicolon, but did not get that it was meant in a strict sense and that it does not allow an expression statement to have more because I read “consists of” but interpreted it as “have”. If something has something it does not mean it can not have something else as well. It was for me like: “If a line of code has an expression terminated by a semicolon then the line of code is an expression statement”. – Patrik Nusszer Jul 18 '19 at 14:50

3 Answers3

5

A return statement and an expression statement are two different things.

Section 6.8.3 of the C standard gives the syntax for an expression statement:

expression-statement:

  • expressionopt;

While section 6.8.6 gives the syntax of a return statement:

jump-statement:

  • goto identifier;
  • continue;
  • break;
  • return expressionopt;

Also, this is not an expression statement (in fact not a statement at all):

int x = 7;

But a declaration.

dbush
  • 205,898
  • 23
  • 218
  • 273
4

This is basically answered by Expression Versus Statement. The key question is: Does a return evaluate to a value (e.g. could you do x = return 5;?). Clearly it does not, so it is a statement, not an expression. Expression statements are just expressions used as statements; if it's not an expression, it can't be an expression statement, so return does not form an expression statement.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
3

No. Expression statements are (optional) expressions followed by ;.

return 5 isn't an expression. That's because it doesn't evaluate to a value (you can't assign return 5 to anything) and because it's specifically defined as a jump statement, which is a type of statement distinct from expression statements.

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142