什么是C ++中的表达式和表达式语句?

编程入门 行业动态 更新时间:2024-10-15 12:29:59
本文介绍了什么是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 ++中的表达式和表达式语句?

本文发布于:2023-11-06 08:09:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563150.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   语句

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!