简体   繁体   English

执行和评估之间有什么区别?

[英]What is the difference between execution and evaluation?

We say that expresssions "evaluate" values and statement orders computer to "execute". 我们说表达“评估”值和语句命令计算机“执行”。 But to me it seems like same terminology. 但对我来说,似乎是相同的术语。 What is the difference between execution and evaluation in C? C中的执行和评估有什么区别?

When a statement is executed then it comes to the action of evaluation of its expressions. 当一个语句被执行时,它将涉及其表达式的评估行为。 First execution takes place and then evaluation. 首先执行然后进行评估。

In the snippet 在片段中

int i = 5, j;
j = 10 + 5*i;

when the statement j = 10 + 5*i; 当陈述j = 10 + 5*i; is executed then evaluation of expressions j , 10 , 5*i , 10 + 5*i and j = 10 + 5*i takes place. 然后执行表达式j10 5*i10 + 5*ij = 10 + 5*i评估。 Note that first three can be evaluated in any order. 请注意,前三个可以按任何顺序进行评估。

It's just a matter of linguistics. 这只是一个语言学问题。 Expressions are evaluated, statements are executed. 表达式被评估,语句被执行。 In both cases we can say that "something gets done", and I wouldn't worry too much about the difference. 在这两种情况下,我们都可以说“有些事情已经完成”,我不会过分担心这种差异。

To clarify: roughly, a statement is a line of code, and an expression is what you can find between brackets in an if() or while() , or on the right side of an equal sign. 澄清:粗略地说,语句是一行代码,表达式是你可以在if()while()或等号右边的括号中找到的。

For example, int x = 2 + 3; 例如, int x = 2 + 3; is a statement that declares a variable x and assigns to it the result of the expression 2 + 3 , that is, 5 . 是一个声明变量x的语句,并为其分配表达式2 + 3的结果,即5

It's really close enough to not matter in almost all cases. 几乎在所有情况下,它都非常接近并不重要。

If we are to be very precise, I'd say that evaluation produces a result value and does not change state, while execution changes state and the result value is either not produced or is incidental and ignored. 如果我们要非常精确,我会说评估会产生一个结果值并且不会改变状态,而执行会改变状态,结果值要么不产生要么是偶然的并被忽略。

Generally speaking, we evaluate expressions, we execute statements. 一般来说,我们评估表达式,我们执行语句。

So, for example, if we have an if statement in C, we first evaluate the condition, then we execute (or not) the body. 因此,例如,如果我们在C中有if语句,我们首先评估条件,然后执行(或不执行)主体。

The confusion is amplified because in C we have expressions that change status (assignment operator, increment/decrement operators) and the statements that are nothing but expressions. 混淆被放大,因为在C中我们有表达式来改变状态(赋值运算符,递增/递减运算符)和只是表达式的语句。

So when you see 所以当你看到

a = b+c;

in C, it's a statement that is executed, but the execution consists of evaluating the expression a=b+c , where the result (non-const reference to a ) is discarded and the side effect ( a changes its value) is important. 在C,它是被执行的一个语句,但该执行由计算表达式的a=b+c ,其中,所述结果(非const引用到a )被丢弃,并且副作用( a改变其值)是很重要的。

The way I have thought of it is: the term 'expression' is carried over from math terminology. 我想到的方式是:“表达”一词来自数学术语。 Evaluation is defined as : 评估定义为

evaluate 评估

To evaluate is to find the value of a numerical or algebraic expression. 评估是要找到数值或代数表达式的值。

Expressions have the following definition : (Boldface added for emphasis) 表达式具有以下定义 :(为强调添加了粗体字)

An expression in a programming language is a combination of one or more explicit values, constants, variables, operators, and functions that the programming language interprets (according to its particular rules of precedence and of association) and computes to produce ("to return", in a stateful environment) another value. 编程语言中的表达式是编程语言解释的一个或多个显式值,常量,变量,运算符和函数的组合(根据其特定的优先级和关联规则)并计算生成(“返回”) ,在有状态的环境中)另一个价值。

In programming, virtually every statement (or every statement worth writing) is composed to of many expressions. 在编程中,几乎每个语句(或每个值得写的语句)都由许多表达式组成。 I have always used evaluation to refer to the computation of these expressions and execution to refer to the computation of the entire statement. 我总是使用评估来指代这些表达式和执行的计算,以引用整个语句的计算。 This often coincides with distinctions around return value and side-effects as well. 这通常与返回值和副作用的区别相吻合。 Full Statements usually have side-effects and expressions usually return a value of some sort. 完整语句通常有副作用,表达式通常会返回某种值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM