一个表达式和一个语句有什么区别? [英] What is the difference between an expression and a statement?

查看:155
本文介绍了一个表达式和一个语句有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能是重复的,但我没有找到足够的答案给我的例子。一般来说,语句和表达式有什么区别?这是我尚未完全区分的区别。我知道一般来说,表达式应该是返回值的任何东西,例如文字或函数。声明通常被称为翻译员的命令,例如打印等等或做...同时。但是,我不明白。

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.

说打印是一个表达式,是不是有意义,因为它是打印输入(输入到输出)的函数?此外,人们通常会说x = 1是一个语句,但是这不能被认为是一个表达式,其中赋值运算符是作用于两个输入的函数,输出是x指的是1?最后,流控制结构如果不能。 。 。否则被认为是一个三参数函数,其中一个其他参数根据第一个输入的真值返回,使其成为一个表达式?

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?

我可能感到困惑,因为我在LISP中有一个背景,一切都是表达。似乎我不能避开大多数编程结构是表达式的核心思想。因此,有人可以解释所谓的陈述和表达之间真正的区别呢?

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?

推荐答案

表达式语句的定义 - 即使在是一种或另一种 - 特定于某种语言和描述它的语法。

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.

嗯,让我们走吧:


  • 一个语句是一些可评估代码 1 表达上下文;和

  • 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.}

例如,我们来看看C中的 printf 这是一个副作用并返回值;通常返回值被忽略。因此 printf 可以显示为一个语句

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!");

和表达式

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

(C中的语句上下文中出现一个返回类型为 void 的函数调用,但这是强制的通过类型系统而不是解析器。)

(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.)

同样,在JavaScript x = 1; x =(y = 2); x = .. 是一个语句,而 y = 2 是一个产生值的表达式。

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.

相比之下,Ruby可以将顶级赋值视为表达式:

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

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

现在我们来一个Python(2.x)的高峰。在这种情况下, print 是一个语句,这就是为什么这些工作和分别不起作用:

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

如果构造 - 是这些语句还是表达式?再次,这取决于特定的语言。在C和Java中,这些都是清楚的说法:没有办法使用替代值。

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.

另一方面,Scala(和Ruby)允许流控制结构用作表达式,尽管它们也可以作为语句显示:

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 另一个条件的生产类是声明,例如C中的函数声明或Java中的类定义,可以说是不是言论;因为以下已经这么分散,所以这是一个很多的注释。

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.

这篇关于一个表达式和一个语句有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆