我问的是关于c#的问题,但我认为它在大多数其他语言中都是一样的。
有人对表达式和语句有很好的定义吗?它们的区别是什么?
我问的是关于c#的问题,但我认为它在大多数其他语言中都是一样的。
有人对表达式和语句有很好的定义吗?它们的区别是什么?
当前回答
声明中,
语句是构造所有c#程序的过程构建块。语句可以声明局部变量或常量,调用方法,创建对象,或为变量、属性或字段赋值。
由花括号括起来的一系列语句构成了一个代码块。方法体就是代码块的一个例子。
bool IsPositive(int number)
{
if (number > 0)
{
return true;
}
else
{
return false;
}
}
c#中的语句通常包含表达式。c#中的表达式是包含文字值、简单名称或操作符及其操作数的代码片段。
表达式,
表达式是可以计算为单个值、对象、方法或名称空间的代码片段。最简单的两种表达式是字面量和简单名称。字面量是一个没有名字的常量值。
int i = 5;
string s = "Hello World";
i和s都是用来标识局部变量的简单名称。当在表达式中使用这些变量时,将检索变量的值并将其用于表达式。
其他回答
表达式是产生一个值的任何东西:2 + 2 语句是程序执行的基本“块”之一。
注意,在C语言中,“=”实际上是一个运算符,它做两件事:
返回右边子表达式的值。 将右边子表达式的值复制到左边的变量中。
下面是一段ANSI C语法的摘录。你可以看到C语言没有很多不同种类的语句……程序中的大多数语句都是表达式语句,即结尾带有分号的表达式。
statement
: labeled_statement
| compound_statement
| expression_statement
| selection_statement
| iteration_statement
| jump_statement
;
expression_statement
: ';'
| expression ';'
;
http://www.lysator.liu.se/c/ANSI-C-grammar-y.html
很简单:表达式的值是一个值,而语句不是。
表达式是返回值的东西,而语句则不是。
例子:
1 + 2 * 4 * foo.bar() //Expression
foo.voidFunc(1); //Statement
两者之间的重要之处在于,您可以将表达式链接在一起,而语句则不能被链接。
表达式:求值为某个值的东西。例如:1 + 2 / x 语句:执行某些操作的代码行。例如:GOTO 100
在最早的通用编程语言(如FORTRAN)中,这种区别是非常明显的。在FORTRAN中,一条语句是一个执行单元,是你所做的一件事。它不被称为“线”的唯一原因是因为有时它跨越多条线。一个表达式本身什么都做不了……你必须把它赋值给一个变量。
1 + 2 / X
是FORTRAN中的一个错误,因为它不做任何事情。你必须对这个表达做些什么:
X = 1 + 2 / X
FORTRAN没有我们今天所知道的语法——这个想法和巴克斯-诺尔表单(BNF)一起被发明出来,作为algolo -60定义的一部分。在这一点上,语义上的区别(“有一个值”和“做某事”)被庄严地体现在语法中:一种短语是表达式,另一种是语句,解析器可以区分它们。
后来语言的设计者模糊了这种区别:他们允许语法表达式做事情,允许有值的语法语句。 现存最早的流行语言例子是C语言。C语言的设计者意识到,如果允许你求一个表达式的值,然后放弃结果,那也没有什么害处。在C语言中,每个语法表达式都可以通过在后面加上分号而变成语句:
1 + 2 / x;
是一个完全合法的声明,即使绝对不会发生任何事情。类似地,在C语言中,表达式可以有副作用——它可以改变一些东西。
1 + 2 / callfunc(12);
因为callfunc可以做一些有用的事情。
一旦您允许任何表达式作为语句,您也可以在表达式中允许赋值操作符(=)。这就是为什么C允许你做
callfunc(x = 2);
这将计算表达式x = 2(将2的值赋给x),然后将该2传递给函数callfunc。
这种表达式和语句的模糊出现在所有的C语言衍生物(C、c++、c#和Java)中,它们仍然有一些语句(如while),但允许几乎任何表达式用作语句(在c#中,只有赋值、调用、自增和自减表达式可以用作语句;参见Scott Wisniewski的回答)。
拥有两个“语法类别”(这是语句和表达式这类东西的专业名称)可能会导致重复工作。例如,C语言中有两种形式的条件句,一种是语句形式
if (E) S1; else S2;
以及表达式形式
E ? E1 : E2
有时人们希望复制不存在的内容:例如,在标准C中,只有语句可以声明一个新的局部变量——但是这种能力非常有用,可以使用 GNU C编译器提供了一个GNU扩展,允许表达式声明局部变量。
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.
这些概念的事实基础是:
表达式:一种语法类别,其实例可以求值。
语句:一种语法类别,其实例可能涉及表达式的求值,并且不能保证求值的结果值(如果有的话)可用。
除了最初几十年的FORTRAN上下文之外,公认答案中表达式和语句的定义显然都是错误的:
Expressions can be unvaluated operands. Values are never produced from them. Subexpressions in non-strict evaluations can be definitely unevaluated. Most C-like languages have the so-called short-circuit evaluation rules to conditionally skip some subexpression evaluations not change the final result in spite of the side effects. C and some C-like languages have the notion of unevaluated operand which may be even normatively defined in the language specification. Such constructs are used to avoid the evaluations definitely, so the remained context information (e.g. types or alignment requirements) can be statically distinguished without changing the behavior after the program translation. For example, an expression used as the operand of the sizeof operator is never evaluated. Statements have nothing to do with line constructs. They can do something more than expressions, depending on the language specifications. Modern Fortran, as the direct descendant of the old FORTRAN, has concepts of executable statements and nonexecutable statements. Similarly, C++ defines declarations as the top-level subcategory of a translation unit. A declaration in C++ is a statement. (This is not true in C.) There are also expression-statements like Fortran's executable statements. To the interest of the comparison with expressions, only the "executable" statements matter. But you can't ignore the fact that statements are already generalized to be constructs forming the translation units in such imperative languages. So, as you can see, the definitions of the category vary a lot. The (probably) only remained common property preserved among these languages is that statements are expected to be interpreted in the lexical order (for most users, left-to-right and top-to-bottom).
(BTW,关于C的材料,我想补充一下[引文],因为我不记得DMR是否有这样的意见。似乎不是,否则就没有理由在C语言的设计中保留功能重复:特别是逗号操作符和语句。)
(以下基本原理并不是对最初问题的直接回应,但我觉得有必要澄清这里已经回答过的一些问题。)
然而,在通用编程语言中,我们是否需要特定类别的“语句”是值得怀疑的:
Statements are not guaranteed to have more semantic capabilities over expressions in usual designs. Many languages have already successfully abandon the notion of statements to get clean, neat and consistent overall designs. In such languages, expressions can do everything old-style statements can do: just drop the unused results when the expressions are evaluated, either by leaving the results explicitly unspecified (e.g. in RnRS Scheme), or having a special value (as a value of a unit type) not producible from normal expression evaluations. The lexical order rules of evaluation of expressions can be replaced by explicit sequence control operator (e.g. begin in Scheme) or syntactic sugar of monadic structures. The lexical order rules of other kinds of "statements" can be derived as syntactic extensions (using hygienic macros, for example) to get the similar syntactic functionality. (And it can actually do more.) On the contrary, statements cannot have such conventional rules, because they don't compose on evaluation: there is just no such common notion of "substatement evaluation". (Even if any, I doubt there can be something much more than copy and paste from existed rules of evaluation of expressions.) Typically, languages preserving statements will also have expressions to express computations, and there is a top-level subcategory of the statements preserved to expression evaluations for that subcategory. For example, C++ has the so-called expression-statement as the subcategory, and uses the discarded-value expression evaluation rules to specify the general cases of full-expression evaluations in such context. Some languages like C# chooses to refine the contexts to simplify the use cases, but it bloats the specification more. For users of programming languages, the significance of statements may confuse them further. The separation of rules of expressions and statements in the languages requires more effort to learn a language. The naive lexical order interpretation hides the more important notion: expression evaluation. (This is probably most problematic over all.) Even the evaluations of full expressions in statements are constraint with the lexical order, subexpressions are not (necessarily). Users should ultimately learn this besides any rules coupled to the statements. (Consider how to make a newbie get the point that ++i + ++i is meaningless in C.) Some languages like Java and C# further constraints the order of evaluations of subexpressions to be permissive of ignorance of evaluation rules. It can be even more problematic. This seems overspecified to users who have already learned the idea of expression evaluation. It also encourages the user community to follow the blurred mental model of the language design. It bloats the language specification even more. It is harmful to optimization by missing the expressiveness of nondeterminism on evaluations, before more complicated primitives are introduced. A few languages like C++ (particularly, C++17) specify more subtle contexts of evaluation rules, as a compromise of the problems above. It bloats the language specification a lot. This goes totally against to simplicity to average users...
为什么是语句?不管怎样,历史已经一团糟了。似乎大多数语言设计者都没有仔细选择。
更糟糕的是,它甚至让一些类型系统爱好者(他们对PL历史不够熟悉)产生了一些误解,认为类型系统必须与操作语义上更基本的规则设计有重要关系。
严肃地说,基于类型的推理在许多情况下并不是那么糟糕,但在这个特殊情况下尤其没有建设性。即使是专家也会把事情搞砸。
For example, someone emphasizes the well-typing nature as the central argument against the traditional treatment of undelimited continuations. Although the conclusion is somewhat reasonable and the insights about composed functions are OK (but still far too naive to the essense), this argument is not sound because it totally ignores the "side channel" approach in practice like _Noreturn any_of_returnable_types (in C11) to encode Falsum. And strictly speaking, an abstract machine with unpredictable state is not identical to "a crashed computer".