前言:
此时同学们对“c语言中的表达式和语句的关系”大致比较关注,同学们都想要学习一些“c语言中的表达式和语句的关系”的相关内容。那么小编同时在网络上网罗了一些有关“c语言中的表达式和语句的关系””的相关知识,希望小伙伴们能喜欢,我们快快来了解一下吧!我们知道,运算符(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 OperatorsRelational OperatorsLogical OperatorsBitwise OperatorsAssignment OperatorsMisc 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 x2 + 3 // operator+ combines values 2 and 3 to produce value 5x = 2 + 3 // 2 + 3 evaluates to value 5, which is then assigned to variable xstd::cout << x // x evaluates to the value of x, which is then printed to the consolefive() // 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
-End-
标签: #c语言中的表达式和语句的关系