解释一个语句和一个表达式在c ++中的区别 [英] Explaining the difference between a statement and an expression in c++

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

问题描述

我正在努力深入了解一个语句表达式之间的区别

但是,即使阅读此答案,我也发现它很混乱b $ b 表达式与语句

查看以下内容:

I am trying to understand thoroughly the difference between a statement and an expression
But i am finding it confusing even after reading this answer Expression Versus Statement
look at the following:

std::cout << "Hello there? " ;  

我可以说这是一个声明,因为它是以 半但是我也可以说

这是一个表达式,因为我有一个 ostream ,一个 输出运算符 字符串文字


,此表达式生成一个左值操作数。

哪一个是正确的?

I could say that it is a statement as it is ending with a semi- colon BUT i could also say
It is an expression since i have an ostream , an output operator and a string literal
and this expression yields a value which is the left hand operand.
Which one is correct?

推荐答案

让我们看看C ++语法能告诉我们什么: p>

Let's see what the C++ grammar can tell us:

statement:
  labeled-statement
  attribute-specifier-seq_opt expression-statement
  attribute-specifier-seq_opt compount-statement
  attribute-specifier-seq_opt selection-statement
  attribute-specifier-seq_opt iteration-statement
  attribute-specifier-seq_opt jump-statement
  declaration-statement
  attribute-specifier-seq_opt try-block

expression-statement:
  expression_opt ';'

所以这是一个声明;特别是一个表达式语句,它由一个(可能为空的)表达式而后跟一个分号组成。换句话说,

So it is a statement; in particular, an "expression statement", which consists of a (potentially empty) expression followed by a semi-colon. In other words,

std::cout << "Hello there? "

是一个表达式,而

std::cout << "Hello there? " ;

是一个陈述。

这篇关于解释一个语句和一个表达式在c ++中的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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