什么是C ++中的表达式和表达式语句? [英] what's an expression and expression statement in c++?

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

问题描述

我已经读到,通常在c ++中的语句以分号结尾;因此这可能有助于解释什么是表达式语句。

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?

在这种情况下,仅仅是语句,表达式语句还是表达式?

In this case, are both just statements or expression statements or expressions?

int x;
x = 0;


推荐答案

表达式为指定运算的一系列运算符和操作数(这是C ++标准中给出的定义)。例如 42 2 + 2 你好,世界 func( argument)。赋值是C ++中的表达式;

An expression is "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.

我没有看到声明一词的定义,但基本上它是执行某些操作的一部分代码。例如,复合语句(由零个或多个其他语句组成,包含在 { ... } 中),if语句, goto语句,return语句和表达式语句。 (在C ++中,但在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.)

术语 statement expression

表达式语句是一种特殊的语句。它由一个可选表达式和一个分号组成。对该表达式求值,并且任何结果都将被丢弃。通常在语句有副作用(否则没有多大意义)时使用此方法,但是您可以在表达式语句中没有副作用的表达式中使用。例如:

An expression statement is 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.)

请注意

return 42;

是一个语句,但不是表达式语句。它包含一个表达式,但是表达式(加上; )并不构成整个语句。

is a statement, but it's not an expression statement. It contains an expression, but the expression (plus the ;) doesn't make up the entire statement.

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

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