请选择 进入手机版 | 继续访问电脑版
  • 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

What is the difference between an expression and a statement?

[复制链接]
菜鸟教程小白 发表于 2022-6-22 22:05:38 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

I know that this might be a duplicate, but I have not found sufficient answers to my examples. In general, what is the difference between statements and expressions? This is a distinction which I have not fully distinguished yet. I know that typically an expressions is supposedly anything that returns a value, such as a literal, or a function. Statements are usually said to be commands to the interpreter, such as "print such and such" or "do . . . while". However, I don't get it.

Wouldn't it make sense to say that print is an expression, since it is a function printing an input (input to output)? Also, people usually say that x = 1 is a statement, but couldn't this be considered an expression where the assignment operator is a function acting on two inputs and the output is that x refers to 1? Finally, couldn't a flow control structure such as if . . . else be considered a three-argument function in which one of the other arguments are returned based upon the truth value of the first input, making it an expression?

I am probably confused because I have a background in LISP, where everything is an expression. It seems that I can't shy away from the thought that most programming constructs are expressions at their core. Thus, could someone explain what really is the difference between a so-called statement and an expression?



Best Answer-推荐答案


The definition of expression and statement - and even if there is such a thing as one or the other - is specific to a particular language and the grammar that describes it.

Well, let's go:

  • A statement is some 'evaluatable code'1 that does not appear in an expression context; and

  • An expression is code that appears in a context where the resulting value can be consumed through substitution of the expression.

{A very loose 'definition', but there is no One Language. While some languages are strict about when side-effects can and cannot occur - and code that executes without result or side-effect is useless - I do not believe discussing such is fundamental to the differences.}

For instance, let's look at printf in C. This is a function that side a side-effect and it returns a value; usually the return value is ignored. Thus printf can appear as both a statement

printf("Hello world!");

and an expression

if (8 == printf("Hello %s!", name)) { // ..

(A function call with a return type of void can only appear in a statement context in C but this is imposed by the type system and not the parser.)

Likewise, take these two lines in JavaScript x = 1; and x = (y = 2);. x = .. is a statement while y = 2 is an expression that yielded a value.

In both of these examples we see that it is the grammar production that determined if it is treated as statement or an expression.

In contrast Ruby can treat the 'top level' assignment as an expression:

[1].map {|x| x = 2}

Now let's take a peak a Python (2.x). In this case print is a statement which is why these work and don't work, respectively:

print "Look ma, no parenthesis!"
x = lambda y: print "Whoops!"       # invalid, print not an expression

And what about at if constructs - are these statements or expressions? Again, it depends on the particular language. In C and Java such are clearly statements: there is no way to use such as a substitute for a value.

On the other hand, Scala (and Ruby) allows such flow control constructs to be used as expressions, although they can also appear as statements:

var emotionalResponse = if (color == "green") {
                          log.cheer()
                          new Cheering()
                        } else {
                          new Tears()
                        }

Whew. That is a lot - and it's not nearly comprehensive. But, back to the 'definition' which can be restated about as so, when taking the various examples above into account:

If the construct in question can occur where a value is required (eg. on the right-hand-side of an assignment, as a function argument, as input to another expression) then it can be treated as an expression; and most definitely is an expression when in such a context. If the construct appears in a location where the value cannot be accessed through substitution then it is (or rather, can act as) a statement.


1 Another class of productions to condition is declarations, such as a function declarations in C or class definitions in Java, and are arguably not statements; since the following is already so fragmented this is as much of a note that gets.

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap