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.
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:
- expression opt
;
While section 6.8.6 gives the syntax of a return
statement:
jump-statement:
goto
identifier;
continue;
break;
return
expression opt;
Also, this is not an expression statement (in fact not a statement at all):
int x = 7;
But a declaration .
This is basically answered by Expression Versus Statement . The key question is: Does a return
evaluate to a value (eg 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.
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 .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.