防爆pression对战声明 [英] Expression Versus Statement

查看:113
本文介绍了防爆pression对战声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问候C#问,但我相信在大多数其他语言中的相同。

I'm asking with regards to c#, but I assume its the same in most other languages.

有没有人有一个良好定义的前pressions 的和的语句的和什么区别?

Does anyone have a good definition of expressions and statements and what the differences are?

推荐答案

前pression:的东西将计算得到一个值。例如: 1 + 2 / X 的结果
声明: code的线,做一些事情。例如: GOTO 100

Expression: Something which evaluates to a value. Example: 1+2/x
Statement: A line of code which does something. Example: GOTO 100

在最早的通用编程语言,如FORTRAN,这种区分是清澈的。在Fortran中,声明是一个执行单位,是你做的事情。它不叫行的唯一原因是因为有时横跨多行。在自己的前$ ​​P $ pssion不能做任何事情......你必须将它分配给一个变​​量。

In the earliest general-purpose programming languages, like FORTRAN, the distinction was crystal-clear. In FORTRAN, a statement was one unit of execution, a thing that you did. The only reason it wasn't called a "line" was because sometimes it spanned multiple lines. An expression on its own couldn't do anything... you had to assign it to a variable.

1 + 2 / X

在FORTRAN一个错误,因为它没有做任何事情。你不得不做与前pression东西:

is an error in FORTRAN, because it doesn't do anything. You had to do something with that expression:

X = 1 + 2 / X

FORTRAN没有语法,我们今天与MDASH知道,这一想法的发明,用巴科斯范式(BNF)一起,为大陵五-60定义的一部分。在这一点上的语义的区别(有值与有所作为)被供奉在的语法的:一种短语,是以前pression和另一个是一个声明,解析器可以区分它们。

FORTRAN didn't have a grammar as we know it today—that idea was invented, along with Backus-Naur Form (BNF), as part of the definition of Algol-60. At that point the semantic distinction ("have a value" versus "do something") was enshrined in syntax: one kind of phrase was an expression, and another was a statement, and the parser could tell them apart.

后来的语言设计者模糊的区别:他们允许语法前pressions做的事情,和他们允许语法语句有值。
仍然生存的最早流行的语言例子是C. C的设计师意识到,没有坏处做,如果你被允许评估一个前pression扔掉的结果。

Designers of later languages blurred the distinction: they allowed syntactic expressions to do things, and they allowed syntactic statements that had values. The earliest popular language example that still survives is C. The designers of C realized that no harm was done if you were allowed to evaluate an expression and throw away the result. In C, every syntactic expression can be a made into a statement just by tacking a semicolon along the end:

1 + 2 / X;

是尽管绝对什么都不会发生完全合法声明。同样,在C,一个前pression可以拥有的副作用的—它可以改变一些东西。

is a totally legit statement even though absolutely nothing will happen. Similarly, in C, an expression can have side-effects—it can change something.

1 + 2 / callfunc(12);

由于 callfunc 可能只是做一些有用的东西。

because callfunc might just do something useful.

一旦你允许任何前pression是一个说法,你还不如让赋值运算符(=)内的前pressions。这就是为什么ç让你做这样的事情。

Once you allow any expression to be a statement, you might as well allow the assignment operator (=) inside expressions. That's why C lets you do things like

callfunc(X = 2);

本评估前pression X = 2(指定的2 x的值),然后通过了(2)以功能 callfunc

This evaluates the expression x = 2 (assigning the value of 2 to x) and then passes that (the 2) to the function callfunc.

EX pressions和语句的这种模糊发生在所有的C衍生物(C,C ++,C#和Java),仍然有一些语句(如,而),但它允许几乎所有的前pression用作声明(在C#中只分配,调用,递增,递减前pressions可以用作报表;见斯科特的Wisniewski的答案)

This blurring of expressions and statements occurs in all the C-derivatives (C, C++, C#, and Java), which still have some statements (like while) but which allow almost any expression to be used as a statement (in C# only assignment, call, increment, and decrement expressions may be used as statements; see Scott Wisniewski's answer).

有两个句法范畴(这是那种事情陈述和前pressions都是技术名称)会导致重复劳动。例如,C有两种形式的条件,则该语句形式的

Having two "syntactic categories" (which is the technical name for the sort of thing statements and expressions are) can lead to duplication of effort. For example, C has two forms of conditional, the statement form

if (E) S1; else S2;

和前任pression形式

and the expression form

E ? E1 : E2

和有时人们希望的复制,是不是有:标准C,例如,只有一个语句可以声明一个新的局部变量—但这种能力是足够有用
GNU C编译器提供了一个GNU扩展,它使前pression声明一个局部变量为好。

And sometimes people want duplication that isn't there: in standard C, for example, only a statement can declare a new local variable—but this ability is useful enough that the GNU C compiler provides a GNU extension that enables an expression to declare a local variable as well.

其他语言的设计者并不喜欢这种重复的,他们早看到了,如果前pressions可能有副作用,以及价值观,那么的语法的语句之间的区别和前pressions是不是所有的有用—使他们摆脱它。哈斯克尔,图标,Lisp语言,和ML都是语言没有语法的语句—他们只有前pressions。即使是一流的循环结构和条件的形式被认为是前pressions,他们有值—但不是很有趣的。

Designers of other languages didn't like this kind of duplication, and they saw early on that if expressions can have side effects as well as values, then the syntactic distinction between statements and expressions is not all that useful—so they got rid of it. Haskell, Icon, Lisp, and ML are all languages that don't have syntactic statements—they only have expressions. Even the class structured looping and conditional forms are considered expressions, and they have values—but not very interesting ones.

这篇关于防爆pression对战声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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