他们晓得,演算符(operator)是表达式(expression)的重要组成部分,表达式能形成句子(statement),但句子除表达式句子(expression statement),除了业务流程掌控句子(flow control statement)。句子形成表达式(function),表达式形成流程(progarm)。
An operationis a mathematical calculation involving zero or more input values, calledoperands. The specific operation to be performed is denoted by the providedoperator. The result of an operation produces an output value.
演算是一类牵涉开始符号或数个输入值的数学计算,称作数组。要继续执行的具体实施由提供更多的演算符则表示。操作方式的结论造成输入值。
Unary operators take one operand. Binaryoperators take two operands, often called left and right.Ternary operators take three operands.
十元演算符选用两个数组。相互依赖演算符选用三个数组,一般来说称作左数组和右数组。Ins13zD演算符选用四个数组。
An expressionis 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 calledevaluation. The value produced is the result of the expression.
表达式是字面量、变量、演算符和表达式调用的组合,这些调用经过求值以生成单个输入值。该输入值的计算称作求值。生成的值是表达式的结论。
An expression statementis an expression that has been turned into a statement by placing a semicolon at the end of the expression.
表达式句子是通过在表达式末尾放置分号而转换为句子的表达式。
A statementis a type of instruction that causes the program to perform some action. Statements are often terminated by a semicolon.
句子是一类使流程继续执行某些操作方式的指令类型。句子一般来说以分号结尾。
A functionis 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 OperatorsRelational OperatorsLogical OperatorsBitwise OperatorsAssignment OperatorsMisc Operators1.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 acompound 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’sprecedence. Using normal mathematical precedence rules (which state that multiplication is resolved before addition), we know that the above expression should evaluate as4 + (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’sassociativitytells 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 expressionis 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 calledevaluation, and the single value produced is called the resultof 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 expressionWherever 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 * cWe 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; intvalue=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-