C++ Review Road 4 - Expressions

The concept of expressions

An expression is composed of one or more operands, and the operands can be connected by operators.

2. Compound expressions

Compound expressions are expressions that contain two or more operators.

[Note] Pay attention to the priority of operators when performing operations on compound expressions .

Third, the priority table of arithmetic operators


Arithmetic Operators (Left Associativity)
operator Function usage

+

-

unary positive sign

unary minus sign

+ expr

- expr

*

/

%

multiplication

division

Surplus

expr * expr

expr / expr

expr % expr

+

-

addition

subtraction

expr + expr

expr - expr

The precedence of arithmetic operators in the table decreases from top to bottom! The above operators all satisfy the left associative law , so when the priorities are the same, they are calculated from left to right .
4. Logical and relational operators

  logical operators and relational operator  
associativity operator Function usage
right logical not ! expr

Left

Left

Left

Left

<

<=

>

>=

less than

less than or equal to

more than the

greater or equal to

expr < expr

expr <= expr

expr > expr

expr  >= expr

Left

Left

==

!=

equal

not equal

expr == expr

expr != expr

Left

&& logical and

expr && expr

Left || logical or expr || expr

[Note] The above 9 operators do not specify the order of operations except for logical and (&&) and logical or (||).

Sequence of operations for logical AND and logical OR

Logical AND: The right-hand operand is evaluated if and only if the left-hand operand is true .

Logical OR: Evaluates the right-hand operand if and only if the left-hand operand is false .

5. Assignment operator

The assignment operator satisfies the right associativity

Assignment operators have lower precedence than relational operators.

[Note] Do not mix assignment operator (=) and equality operator (==)! Errors that occur if mixed use are undetectable.

if(i == j)
{
    /*Corresponding operation*/
}

Determine whether i and j are equal, and if they are equal, perform the corresponding operation. Otherwise it will not enter the if conditional statement.

if(i = j)
{
    /*Corresponding operation*/
}

This is to assign the value of j to i. As long as the value of j is not equal to 0, you can enter the if conditional statement to perform the corresponding operation.

6. Increment and Decrement Operators

Increment operator (++) and decrement operator (--), these two operators add one and subtract one to the object respectively.

They come in two versions, a pre-version (++i) and a post-version (i--)

Let's take a look at the difference between the two versions

Pre-version:

int i = 0, j;
j = ++ i; // j = 1 , i = 1;

Post version:

int i = 0, j;
j = i++;  // j = 0, i = 1;
From the above examples, we can know that the pre-version returns the value after the increment (or decrement), but the post -version returns the value before the increment (or decrement) .

[Note] Under the same conditions , the efficiency of the pre-version is usually higher than that of the post-version . Because the post-version needs to save the original value and needs a temporary variable , but the pre-version value connection is to return the incremented (or decremented) value without creating a temporary variable, so the efficiency will be higher.

Mixing dereference and increment operators

see an example 

*p++;

【注意】后置版本的优先级高于解引用运算符,所以以上的表达式可以写成这样*(p++); 这个表达式首先将p的值加一, 然后返回p的初始值的副本作为其结果,然后*解引用的就是p未加之前的值

七、sizeof运算符

sizeof运算符返回的是一条表达式或者一个类型名字所占的字节数。sizeof运算符满足右结合律。

八、逗号运算符

逗号运算符含有两个运算对象,按照从左向右的顺序依次求值。

【注意】逗号运算符规定了运算对象的求值顺序。

首先对左侧的表达式求值,然后将求值结果丢弃,逗号运算符的真正结果是右侧表达式的值。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324515414&siteId=291194637