什么是 C++ 中的表达式和表达式语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7479946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what's an expression and expression statement in c++?
提问by user942451
I've read that usually statements in c++ end with a semi-colon; so that might help explain what an expression statement would be. But then what would you call an expression by giving an example?
我读过 C++ 中的语句通常以分号结尾;所以这可能有助于解释表达式语句是什么。但是,通过举个例子,你会怎么称呼表达式呢?
In this case, are both just statements or expression statements or expressions?
在这种情况下,两者都只是语句还是表达式语句或表达式?
int x;
x = 0;
采纳答案by Keith Thompson
An expressionis "a sequence of operators and operands that specifies a computation" (that's the definition given in the C++ standard). Examples are 42
, 2 + 2
, "hello, world"
, and func("argument")
. Assignments are expressions in C++; so are function calls.
一个表达式是(这是在C ++标准给出的定义)“运算符和操作数,指定计算的序列”。例子是42
,2 + 2
,"hello, world"
,和func("argument")
。赋值是 C++ 中的表达式;函数调用也是如此。
I don't see a definition for the term "statement", but basically it's a chunk of code that performs some action. Examples are compound statements (consisting of zero or more other statements included in {
... }
), if statements, goto statements, return statements, and expression statements. (In C++, but not in C, declarations are classified as statements.)
我没有看到术语“语句”的定义,但基本上它是执行某些操作的一段代码。实例是复合语句(包括零个或包括在多个其它语句{
... }
)中,如果语句,goto语句,返回语句和表达式语句。(在 C++ 中,而不是在 C 中,声明被归类为语句。)
The terms statementand expressionare defined very precisely by the language grammar.
术语语句和表达式由语言语法非常精确地定义。
An expression statementis a particular kind of statement. It consists of an optional expression followed by a semicolon. The expression is evaluated and any result is discarded. Usually this is used when the statement has side effects (otherwise there's not much point), but you can have a expression statement where the expression has no side effects. Examples are:
一个表达式语句是一种特殊的语句。它由一个可选的表达式和一个分号组成。计算表达式并丢弃任何结果。通常这在语句有副作用时使用(否则没有多大意义),但您可以使用表达式没有副作用的表达式语句。例子是:
x = 42; // the expression happens to be an assignment
func("argument");
42; // no side effects, allowed but not useful
; // a null statement
The null statement is a special case. (I'm not sure why it's treated that way; in my opinion it would make more sense for it to be a disinct kind of statement. But that's the way the standard defines it.)
null 语句是一种特殊情况。(我不确定为什么要这样对待它;在我看来,将它作为一种不明确的陈述会更有意义。但这就是标准定义它的方式。)
Note that
注意
return 42;
is a statement, but it's notan expression statement. It contains an expression, but the expression (plus the ;
) doesn't make up the entire statement.
是一个语句,但它不是一个表达式语句。它包含一个表达式,但该表达式(加上;
)并不构成整个语句。
回答by Mateen Ulhaq
These are expressions (remember math?):
这些是表达式(还记得数学吗?):
1
6 * 7
a + b * 3
sin(3) + 7
a > b
a ? 1 : 0
func()
mystring + gimmeAString() + std::string("\n")
The following are all statements:
以下为所有声明:
int x; // Also a declaration.
x = 0; // Also an assignment.
if(expr) { /*...*/ } // This is why it's called an "if-statement".
for(expr; expr; expr) { /*...*/ } // For-loop.
A statement is usually made up of an expression:
一个语句通常由一个表达式组成:
if(a > b) // a > b is an expr.
while(true) // true is an expr.
func(); // func() is an expr.
回答by ilgaar
To understand what is an expression statement, you should first know what is an expression and what is an statement.
要了解什么是表达式语句,首先要知道什么是表达式,什么是语句。
An expressionin a programming language is a combination of one or more explicit values, constants, variables, operators, and functionsthat 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. This process, as for mathematical expressions, is called evaluation.
编程语言中的表达式是编程语言解释(根据其特定的优先级和关联规则)并计算生成(“返回”)的一个或多个显式值、常量、变量、运算符和函数的组合。,在有状态环境中)另一个值。对于数学表达式,这个过程称为求值。
Source: https://en.wikipedia.org/wiki/Expression_(computer_science)
来源:https: //en.wikipedia.org/wiki/Expression_(computer_science)
In other words expressions are a sort of data items. They can have single or multiple entities like constants and variables. These entities may be related or connected to each other by operators. Expressions may or may not have side effects, in that they evaluate to something by means of computation which changes a state. For instance numbers, things that look like mathematical formulas and calculations, assignments, function calls, logical evaluations, strings and string operations are all considered expressions.
换句话说,表达式是一种数据项。它们可以有单个或多个实体,如常量和变量。这些实体可能通过运营商相互关联或连接。表达式可能有也可能没有副作用,因为它们通过改变状态的计算来评估某些东西。例如数字、看起来像数学公式和计算的东西、赋值、函数调用、逻辑评估、字符串和字符串操作都被视为表达式。
function calls: According to MSDN, function calls are considered expressions. A function call is an expression that passes control and arguments (if any) to a function and has the form:
expression (expression-list opt)
which is invoked by the ( )
function operator.
函数调用:根据 MSDN,函数调用被认为是表达式。函数调用是将控制和参数(如果有)传递给函数的表达式,其形式为:
expression (expression-list opt)
由( )
函数运算符调用。
source: https://msdn.microsoft.com/en-us/library/be6ftfba.aspx
来源:https: //msdn.microsoft.com/en-us/library/be6ftfba.aspx
Some examples of expressions are:
一些表达式的例子是:
46
18 * 3 + 22 / 2
a = 4
b = a + 3
c = b * -2
abs(c)
b >= c
c
"a string"
str = "some string"
strcat(str, " some thing else")
str2 = "some string" + " some other string" // in C++11 using string library
Statementsare fragments of a program that execute in sequence and cause the computer to carry out some definite action. Some C++ statement types are:
语句是按顺序执行并使计算机执行某些确定操作的程序片段。一些 C++ 语句类型是:
- expression statements;
- compound statements;
- selection statements;
- iteration statements;
- jump statements;
- declaration statements;
- try blocks;
- atomic and synchronized blocks (TM TS).
- 表达式语句;
- 复合语句;
- 选择声明;
- 迭代语句;
- 跳转语句;
- 声明声明;
- 尝试块;
- 原子和同步块 (TM TS)。
Source: http://en.cppreference.com/w/cpp/language/statements
来源:http: //en.cppreference.com/w/cpp/language/statements
I've read usually statements in c++ ends with a semicon;
我读过 C++ 中的语句通常以分号结尾;
Yes usually! But not always. Consider the following piece of code which is a compound statement but does not end with a semicolon, rather it is enclosed between two curly braces:
是的,通常!但不总是。考虑下面这段代码,它是一个复合语句,但不以分号结尾,而是包含在两个大括号之间:
{ // begining of a compound statement
int x; // A declaration statement
int y;
int z;
x = 2; // x = 2 is an expression, thus x = 2; with the trailing semicolon is an expression statement
y = 2 * x + 5;
if(y == 9) { // A control statement
z = 52;
} else { // A branching statement of a control statement
z = 0;
}
} // end of a compound statement
By now, as you might be guessing, an expression statement is any statement that has an expression followed by a semicolon. According to MSDN an expression statementis a statement that causes the expressions to be evaluated. No transfer of control or iteration takes place as a result of an expression statement.
到现在为止,您可能已经猜到了,表达式语句是任何具有表达式后跟分号的语句。根据 MSDN,表达式语句是导致表达式被求值的语句。作为表达式语句的结果,不会发生控制或迭代的转移。
Source: https://msdn.microsoft.com/en-us/library/s7ytfs2k.aspx
来源:https: //msdn.microsoft.com/en-us/library/s7ytfs2k.aspx
Some Examples of expression statements:
表达式语句的一些示例:
x = 4;
y = x * x + 10;
radius = 5;
pi = 3.141593;
circumference = 2. * pi * radius;
area = pi * radius * radius;
Therefore the following can not be considered expression statements since they transfer the control flow to another part of a program by calling a function:
因此,以下不能被视为表达式语句,因为它们通过调用函数将控制流转移到程序的另一部分:
printf("The control is passed to the printf function");
y = pow(x, 2);
side effects: A side effect refers to the modification of a state. Such as changing the value of a variable, writing some data on a disk showing a menu in the User Interface, etc.
副作用:副作用是指状态的改变。例如更改变量的值,在磁盘上写入一些数据,显示用户界面中的菜单等。
Source: https://en.wikipedia.org/wiki/Side_effect_(computer_science)
来源:https: //en.wikipedia.org/wiki/Side_effect_(computer_science)
Note that expression statements don't need to have side effects. That is they don't have to change or modify any state. For example if we consider a program's control flow as a state which could be modified, then the following expression statements won't have any side effects over the program's control flow:
请注意,表达式语句不需要有副作用。也就是说,他们不必更改或修改任何状态。例如,如果我们将程序的控制流视为可以修改的状态,那么以下表达式语句不会对程序的控制流产生任何副作用:
a = 8;
b = 10 + a;
k++;
Wheres the following expression statement would have a side effect, since it would pass the control flow to sqrt() function, thus changing a state:
以下表达式语句会产生副作用,因为它将控制流传递给 sqrt() 函数,从而改变状态:
d = sqrt(a); // The control flow is passed to sqrt() function
If we consider the value of a variable as a state as well, modifying it would be a side effect thus all of expression statements above have side effects, because they all modify a state. An expression statement that does not have any side effect is not very useful. Consider the following expression statements:
如果我们将变量的值也视为状态,则修改它会产生副作用,因此上述所有表达式语句都有副作用,因为它们都修改了状态。没有任何副作用的表达式语句不是很有用。考虑以下表达式语句:
x = 7; // This expression statement sets the value of x to 7
x; // This expression statement is evaluated to 7 and does nothing useful
In the above example x = 7;
is a useful expression statement for us. It sets the value of x to 7 by =
the assignment operator. But x;
evaluates to 7 and it doesn't do anything useful.
在上面的例子中x = 7;
是一个对我们有用的表达式语句。它通过=
赋值运算符将 x 的值设置为 7 。但是x;
评估为 7 并且它没有做任何有用的事情。
回答by Austin Henley
According to The C++ Programming Language by Bjarne Stroustrup Special(3rd) Edition, a statement is basically any declaration, function call, assignment, or conditional. Though, if you look at the grammar, it is much more complicated than that. An expression, in simple terms, is any math or logical operation(s).
根据 Bjarne Stroustrup Special(3rd) Edition 的 The C++ Programming Language,语句基本上是任何声明、函数调用、赋值或条件。但是,如果您查看语法,它会比这复杂得多。简单来说,表达式是任何数学或逻辑运算。
The wikipedia links that ok posted in his answer can be of help too.
在他的回答中发布的维基百科链接也可以提供帮助。
回答by donkey
In my opinion,
在我看来,
a statement*states* the purpose of a code block. i.e. we say this block of code if(){}
is an if-statement, or this x=42;
is an expression statement. So code such as 42;
serves no purporse, therefore, this is *not* a statement.
一个声明* *国家代码块的目的。即我们说这段代码if(){}
是一个 if 语句,或者这x=42;
是一个表达式语句。因此,诸如此类的代码42;
没有任何意义,因此,这*不是*声明。
and,
和,
an expressionis any legal combination of symbols that represents a value (Credit to Webopedia); it combines variables and constants to produce new values(Quoted from Chapter 2 in The C Programming Language). Therefore, it also has a mathematical connotation. For instance, number 42
in x=42;
is an expression (x=42;
is not an expression but rather an expression statement), or func(x)
is an expression because it will evaluate to something. On the contrary, int x;
is not an expression because it is not representing any value.
一个表达式是表示一个值(信用到的符号的任何合法组合Webopedia); 它结合了变量和常量来产生新的值(引自 The C Programming Language 第 2 章)。因此,它也具有数学内涵。例如, number 42
inx=42;
是一个表达式(x=42;
不是一个表达式而是一个表达式语句),或者func(x)
是一个表达式,因为它会计算出一些东西。相反,int x;
不是表达式,因为它不代表任何值。
回答by Rick
回答by Coffee
An expression is part of a statement, OR a statement itself.
表达式是语句的一部分,或语句本身。
int x;
is a statement and expression.
int x;
是一个语句和表达式。
See this : http://en.wikipedia.org/wiki/Expression_%28programming%29
看到这个:http: //en.wikipedia.org/wiki/Expression_%28programming%29