百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程字典 > 正文

C++运算符、表达式、语句及相互关系

toyiye 2024-05-09 18:41 20 浏览 0 评论

我们知道,运算符(operator)是表达式(expression)的组成部分,表达式可以构成语句(statement),但语句除了表达式语句(expression statement),还有流程控制语句(flow control statement)。语句构成函数(function),函数构成程序(progarm)。

An operation is a mathematical calculation involving zero or more input values, called operands. The specific operation to be performed is denoted by the provided operator. The result of an operation produces an output value.

运算是一种涉及零个或多个输入值的数学计算,称为操作数。要执行的具体操作由提供的操作符表示。操作的结果产生输出值。

Unary operators take one operand. Binary operators take two operands, often called left and right. Ternary operators take three operands.

一元运算符采用一个操作数。二元运算符采用两个操作数,通常称为左操作数和右操作数。三值运算符采用三个操作数。

An expression is a combination of literals, variables, operators, and function calls that are evaluated to produce a single output value. The calculation of this output value is called evaluation. The value produced is the result of the expression.

表达式是字面量、变量、运算符和函数调用的组合,这些调用经过求值以生成单个输出值。该输出值的计算称为求值。生成的值是表达式的结果

An expression statement is an expression that has been turned into a statement by placing a semicolon at the end of the expression.

表达式语句是通过在表达式末尾放置分号而转换为语句的表达式。

A statement is a type of instruction that causes the program to perform some action. Statements are often terminated by a semicolon.

语句是一种使程序执行某些操作的指令类型。语句通常以分号结尾。

A function is a collection of statements that execute sequentially.

函数是按顺序执行的语句的集合。

1 运算符

The specific operation to be performed is denoted by a construct (typically a symbol or pair of symbols) called an operator.

要执行的特定操作由称为运算符的构造(通常是一个符号或一对符号)表示。

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C++ is rich in built-in operators and provide the following types of operators ?

运算符是一种符号,它告诉编译器执行特定的数学或逻辑操作。C++有丰富的内置操作符,并提供以下类型的操作符?

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Misc Operators

1.1 operator’s arity

The number of operands that an operator takes as input is called the operator’s arity . Operators in C++ come in three different arities:

操作符作为输入的操作数数被称为操作符的元。C++中的运算符有三种不同的算术:

Unary operators act on one operand. An example of a unary operator is the - operator. For example, given -5, operator- takes literal operand 5 and flips its sign to produce new output value -5.

一元运算符作用于一个操作数。一元运算符的一个示例是-运算符。例如,给定-5,运算符-接受文字操作数5并翻转其符号以生成新的输出值-5。

Binary operators act on two operands (known as left and right). An example of a binary operator is the + operator. For example, given 3 + 4, operator+ takes the left operand (3) and the right operand (4) and applies mathematical addition to produce new output value 7. The insertion (<<) and extraction (>>) operators are binary operators, taking std::cout or std::cin on the left side, and the item to output or variable to input to on the right side.

二元运算符作用于两个操作数(称为左操作数和右操作数)。二元运算符的一个示例是+运算符。例如,给定3+4,运算符+取左操作数(3)和右操作数(4),并应用数学加法生成新的输出值7。插入(<<)和提取(>>)运算符是二进制运算符,在左侧取std::cout或std::cin,在右侧取要输出的项或要输入的变量。

Ternary operators act on three operands. There is only one of these in C++, whic is expression1 ? expression2 : expression3.

三元运算符作用于三个操作数。C++中只有一个这样的版本,表达式1 ? 表达式2 : 表达式3。

Note that some operators have more than one meaning depending on how they are used. For example, operator- has two contexts. It can be used in unary form to invert a number’s sign (e.g. to convert 5 to -5, or vice versa), or it can be used in binary form to do subtraction (e.g. 4 - 3).

请注意,一些运算符有多个含义,具体取决于它们的使用方式。例如,operator-有两个上下文。它可以用一元形式反转数字的符号(例如,将5转换为-5,反之亦然),也可以用二进制形式进行减法(例如,4-3)。

1.2 Operators can be chained

Operators can be chained together such that the output of one operator can be used as the input for another operator. For example, given the following: 2 * 3 + 4, the multiplication operator goes first, and converts left operand 2 and right operand 3 into new value 6 (which becomes the left operand for the plus operator). Next, the plus operator executes, and converts left operand 6 and right operand 4 into new value 10.

操作符可以链接在一起,这样一个操作符的输出可以用作另一个操作符的输入。例如,给定以下值:2*3+4,乘法运算符首先执行,并将左操作数2和右操作数3转换为新值6(该值成为加号运算符的左操作数)。接下来,执行加号运算符,并将左操作数6和右操作数4转换为新值10。

We’ll talk more about the order in which operators execute when we do a deep dive into the topic of operators. For now, it’s enough to know that the arithmetic operators execute in the same order as they do in standard mathematics: Parenthesis first, then Exponents, then Multiplication and Division, then Addition and Subtraction. This ordering is sometimes abbreviated PEMDAS, or expanded to the mnemonic “Please Excuse My Dear Aunt Sally”.

当我们深入研究操作符主题时,我们将更多地讨论操作符的执行顺序。现在,只需知道算术运算符的执行顺序与标准数学中的相同:首先是括号,然后是指数,然后是乘法和除法,然后是加法和减法。这种排序有时缩写为PEMDAS,或扩展为助记符“请原谅我亲爱的Sally阿姨”。

1.3 Operator precedence

Now, let’s consider a more complicated expression, such as 4 + 2 * 3. An expression that has multiple operators is called a compound expression. In order to evaluate this compound expression, we must understand both what the operators do, and the correct order to apply them. The order in which operators are evaluated in a compound expression is determined by an operator’s precedence. Using normal mathematical precedence rules (which state that multiplication is resolved before addition), we know that the above expression should evaluate as 4 + (2 * 3) to produce the value 10.

现在,让我们考虑一个更复杂的表达式,例如4+2*3。具有多个运算符的表达式称为复合表达式。为了计算这个复合表达式,我们必须了解操作符的作用以及应用它们的正确顺序。复合表达式中运算符的求值顺序由运算符的优先级决定。使用普通的数学优先规则(即乘法在加法之前得到解决),我们知道上述表达式的计算结果应为4+(2*3),以生成值10。

In C++, when the compiler encounters an expression, it must similarly analyze the expression and determine how it should be evaluated. To assist with this, all operators are assigned a level of precedence. Operators with the highest level of precedence are evaluated first.

在C++中,当编译器遇到一个表达式时,它必须类似地分析该表达式,并确定应该如何对其求值。为了帮助实现这一点,所有操作符都被分配了一个优先级。优先级别最高的运算符将首先求值。

1.4 Operator associativity

What happens if two operators in the same expression have the same precedence level? For example, in the expression 3 * 4 / 2, the multiplication and division operators are both precedence level 5. In this case, the compiler can’t rely upon precedence alone to determine how to evaluate the result.

如果同一表达式中的两个运算符具有相同的优先级,会发生什么情况?例如,在表达式3×4/2中,乘法和除法运算符都是优先级5。在这种情况下,编译器不能仅依靠优先级来确定如何计算结果。

If two operators with the same precedence level are adjacent to each other in an expression, the operator’s associativity tells the compiler whether to evaluate the operators from left to right or from right to left. The operators in precedence level 5 have an associativity of left to right, so the expression is resolved from left to right: (3 * 4) / 2 = 6.

如果一个表达式中有两个具有相同优先级的运算符彼此相邻,则该运算符的结合性告诉编译器是从左到右还是从右到左求值。优先级级别算术运算符具有从左到右的结合性性,因此表达式是从左到右解析的:(3*4)/2=6。

2 expression, expression statement, the order of evaluation

An expression is a combination of literals, variables, operators, and function calls that can be executed to produce a singular value. The process of executing an expression is called evaluation, and the single value produced is called the result of the expression.

表达式是字面量、变量、运算符和函数调用的组合,可以执行这些调用来生成单个值。执行表达式的过程称为求值,生成的单个值称为表达式的结果

When an expression is evaluated, each of the terms inside the expression are evaluated, until a single value remains.

计算表达式时,将计算表达式中的每个项,直到保留单个值为止。

Here are some examples of different kinds of expressions, with comments indicating how they evaluate:

下面是一些不同类型表达式的示例,并附有说明其计算方式的注释:

2               // 2 is a literal that evaluates to value 2
"Hello world!"  // "Hello world!" is a literal that evaluates to text "Hello world!"
x               // x is a variable that evaluates to the value of x
2 + 3           // operator+ combines values 2 and 3 to produce value 5
x = 2 + 3       // 2 + 3 evaluates to value 5, which is then assigned to variable x
std::cout << x  // x evaluates to the value of x, which is then printed to the console
five()          // evaluates to the return value of function five()

As you can see, literals evaluate to their own values. Variables evaluate to the value of the variable. In the context of an expression, function calls evaluate to whatever value the function returns. And operators (such as operator+) let us combine multiple values together to produce a new value.

正如您所看到的,字面量的计算结果是它们自己的值。变量计算为变量的值。在表达式的上下文中,函数调用的计算结果是函数返回的任何值。运算符(例如运算符+)让我们将多个值组合在一起以生成新值。

2.1 expression statement

Note that expressions do not end in a semicolon, and cannot be compiled by themselves. For example, if you were to try compiling the expression x = 5, your compiler would complain (probably about a missing semicolon). Rather, expressions are always evaluated as part of statements.

请注意,表达式不以分号结尾,不能自行编译。例如,如果要编译表达式x=5,编译器会抱怨(可能是因为缺少分号)。相反,表达式总是作为语句的一部分进行计算。

For example, take this statement:

例如,以以下语句为例:

int x = 2+3; // 2 + 3 is an expression that has no semicolon -- 
the semicolon is at the end of the statement containing the expression

Wherever you can use a single value in C, you can use an expression instead, and the expression will be evaluated to produce a single value.

只要在C中可以使用单个值,就可以使用表达式,表达式将被求值以生成单个值。

An expression statement is a statement that consists of an expression followed by a semicolon. When the statement is executed, the expression will be evaluated (and the result of the expression will be discarded).

表达式语句是由表达式后跟分号组成的语句。执行语句时,将对表达式求值(表达式的结果将被丢弃)。

What is the difference between a statement and an expression?

语句和表达式之间有什么区别?

Statements are used when we want the program to perform an action. Expressions are used when we want the program to calculate a value.

当我们希望程序执行操作时,使用语句。当我们希望程序计算值时,使用表达式。

2.2 The order of evaluation of expressions

Consider the following expression:

考虑以下表达式:

a + b * c

We know from the precedence and associativity rules above that this expression will evaluate as if we had typed:

根据上面的优先级和结合性规则,我们知道此表达式的计算结果将与我们键入的一样:

a + (b * c)

If a is 1, b is 2, and c is 3, this expression will evaluate to the answer 7.

如果a为1,b为2,c为3,则此表达式的计算结果为7。

However, the precedence and associativity rules only tell us how operators evaluate in relation to other operators. It does not tell us anything about the order in which the rest of the expression evaluates. For example, does variable a, b, or c get evaluated first?

然而,优先级结合性规则只告诉我们操作符相对于其他操作符的计算方式。它没有告诉我们表达式其余部分的求值顺序。例如,变量a、b或c是否首先求值?

Perhaps surprisingly, in many cases, the order of evaluation of any part of a compound expression (including function calls and argument evaluation) is unspecified. In such cases, the compiler is free to choose any evaluation order it believes is optimal.

也许令人惊讶的是,在许多情况下,复合表达式任何部分的求值顺序(包括函数调用和参数求值)都没有指定。在这种情况下,编译器可以自由选择它认为是最优的任何求值顺序。

Outside of the operator precedence and associativity rules, assume that the parts of an expression could evaluate in any order. Ensure that the expressions you write are not dependent on the order of evaluation of those parts.

在运算符优先级和结合性规则之外,假设表达式的各个部分可以按任何顺序求值。确保您编写的表达式不依赖于这些部分的求值顺序。

#include <iostream>

int add(int x, int y)
{
    return x + y;
}

int main()
{
    int x=5;
    int value=add(x, ++x); // is this 5 + 6, or 6 + 6?
    // It depends on what order your compiler evaluates the function arguments in

    std::cout << value << '\n'; // value could be 11 or 12, depending on how the above line evaluates!
    return 0;
}

The C++ standard does not define the order in which function arguments are evaluated. If the left argument is evaluated first, this becomes a call to add(5, 6), which equals 11. If the right argument is evaluated first, this becomes a call to add(6, 6), which equals 12! Note that this is only a problem because one of the arguments to function add() has a side effect.

C++标准没有定义函数参数的求值顺序。如果首先计算左参数,则这将成为对加法(5,6)的调用,加法等于11。如果首先计算正确的参数,这将成为对add(6,6)的调用,它等于12!请注意,这只是一个问题,因为函数add()的一个参数有副作用。

The C++ standard intentionally does not define these things so that compilers can do whatever is most natural (and thus most performant) for a given architecture.

C++标准有意不定义这些东西,以便编译器可以对给定的体系结构执行任何最自然(因而也是最性能)的操作。

So, don’t use a variable that has a side effect applied to it more than once in a given statement. If you do, the result may be undefined.

所以,不要在给定语句中多次使用具有副作用的变量。如果这样做,结果可能未定义。

3 Control Flow and Error Handling statements

3.1 Conditionals and if statements

3.2 Switch statement

3.3 Loops and while statements

3.4 Do while statements

3.5 For statements

3.6 Break and continue statements

3.7 Goto statements

3.8 try throw catch statements

ref

https://www.learncpp.com/

https://www.tutorialspoint.com/cplusplus/cpp_operators.htm

-End-

相关推荐

为何越来越多的编程语言使用JSON(为什么编程)

JSON是JavascriptObjectNotation的缩写,意思是Javascript对象表示法,是一种易于人类阅读和对编程友好的文本数据传递方法,是JavaScript语言规范定义的一个子...

何时在数据库中使用 JSON(数据库用json格式存储)

在本文中,您将了解何时应考虑将JSON数据类型添加到表中以及何时应避免使用它们。每天?分享?最新?软件?开发?,Devops,敏捷?,测试?以及?项目?管理?最新?,最热门?的?文章?,每天?花?...

MySQL 从零开始:05 数据类型(mysql数据类型有哪些,并举例)

前面的讲解中已经接触到了表的创建,表的创建是对字段的声明,比如:上述语句声明了字段的名称、类型、所占空间、默认值和是否可以为空等信息。其中的int、varchar、char和decimal都...

JSON对象花样进阶(json格式对象)

一、引言在现代Web开发中,JSON(JavaScriptObjectNotation)已经成为数据交换的标准格式。无论是从前端向后端发送数据,还是从后端接收数据,JSON都是不可或缺的一部分。...

深入理解 JSON 和 Form-data(json和formdata提交区别)

在讨论现代网络开发与API设计的语境下,理解客户端和服务器间如何有效且可靠地交换数据变得尤为关键。这里,特别值得关注的是两种主流数据格式:...

JSON 语法(json 语法 priority)

JSON语法是JavaScript语法的子集。JSON语法规则JSON语法是JavaScript对象表示法语法的子集。数据在名称/值对中数据由逗号分隔花括号保存对象方括号保存数组JS...

JSON语法详解(json的语法规则)

JSON语法规则JSON语法是JavaScript对象表示法语法的子集。数据在名称/值对中数据由逗号分隔大括号保存对象中括号保存数组注意:json的key是字符串,且必须是双引号,不能是单引号...

MySQL JSON数据类型操作(mysql的json)

概述mysql自5.7.8版本开始,就支持了json结构的数据存储和查询,这表明了mysql也在不断的学习和增加nosql数据库的有点。但mysql毕竟是关系型数据库,在处理json这种非结构化的数据...

JSON的数据模式(json数据格式示例)

像XML模式一样,JSON数据格式也有Schema,这是一个基于JSON格式的规范。JSON模式也以JSON格式编写。它用于验证JSON数据。JSON模式示例以下代码显示了基本的JSON模式。{"...

前端学习——JSON格式详解(后端json格式)

JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于JavaScriptProgrammingLa...

什么是 JSON:详解 JSON 及其优势(什么叫json)

现在程序员还有谁不知道JSON吗?无论对于前端还是后端,JSON都是一种常见的数据格式。那么JSON到底是什么呢?JSON的定义...

PostgreSQL JSON 类型:处理结构化数据

PostgreSQL提供JSON类型,以存储结构化数据。JSON是一种开放的数据格式,可用于存储各种类型的值。什么是JSON类型?JSON类型表示JSON(JavaScriptO...

JavaScript:JSON、三种包装类(javascript 包)

JOSN:我们希望可以将一个对象在不同的语言中进行传递,以达到通信的目的,最佳方式就是将一个对象转换为字符串的形式JSON(JavaScriptObjectNotation)-JS的对象表示法...

Python数据分析 只要1分钟 教你玩转JSON 全程干货

Json简介:Json,全名JavaScriptObjectNotation,JSON(JavaScriptObjectNotation(记号、标记))是一种轻量级的数据交换格式。它基于J...

比较一下JSON与XML两种数据格式?(json和xml哪个好)

JSON(JavaScriptObjectNotation)和XML(eXtensibleMarkupLanguage)是在日常开发中比较常用的两种数据格式,它们主要的作用就是用来进行数据的传...

取消回复欢迎 发表评论:

请填写验证码