C ++ Chinese operator priority learning annotation

xiaoxiao2021-04-01  212

Lift the priority of the operator, many of the people who know C will think: What is the difficulty? Not who is the priority! Who is it? In this way, the priority of the operator is not a big problem, but for an initiator, it is often easy to confuse and make mistakes. For a person who knows C , I believe it will be occasionally fell above, and I will continue to read it.

The confusion brought about "priority high operation"

The priority of the C operator has a table, the operator is classified, and this table does not need to die hard, as long as there is a rough outline to be OK. For example, it should be remembered that the lowest priority is a comma operator, followed by an assignment operator, and then the second is a three-mean operator. The priority of the relationship operator is higher than the logical operator (excluding logical non-computational), the priority of the arithmetic operator is higher than the relational operator, like and - the priority is high than the front, but the highest It must be (). After you know these, you must have a guideline in your mind: the priority is high. Then look at an example:

INT x = 1, y = 0;

! x && x y && y;

The above statement appeared! , &&, , These four operators, then the problem is coming, who is it?

There is a classmate surname Cai stands up, operators are the highest priority here, the reason should be the first to count , not before calculating y, then count! X, calculate x y, finally They && get up. According to the idea of ​​Cai classmates, the result of the second step is 0 && x y && 1, because && is strictly calculated, there is a result of 0 results from both 0, so do not need to calculate x y, the result of the entire statement is: fake. According to the above-mentioned Cai classmates, the value of Y is 1, this is right?

A classmate who surnamed stood up and retorted. I think it should be calculated first! X, if the value is false, it does not need to be calculated, the last result is false. If the value is true, then the X Y is calculated, if the value is true, then calculate y, otherwise the final result is also false.

Cai Xongxiao did not serve, high-class students, you think and! Who is high priority? The high classmates replied, of course, high. Cai Xi Xu then asked, why should you calculate it first! The high classmate can't answer.

Yes, why do you want to count first!

Package determine the priority method

The high classmate is correct, why? Below I explain it to you. When multiple priority different operators, in order not confused, you can add parentheses, which is divided into hierarchical, the same level considering the combination problem, when it is determined to count, it is then Insert in the block. For example, the above example, we can add parentheses: from left to right, because! Is much higher than the && priority, there is (!!), But because && ratio low priority, there is (x y), is higher than the &&, so ( Y). This makes the entire form becomes: (! X) && (x y) && ( y), the outermost layer is two && operation, because &&'s combination is from left to right, so the above It can be seen as: A && B && C, first calculate A, then calculate B, and finally C. Due to x = 1, then! X is false, and there is no need to calculate again, and the value of the entire statement is false. After the execution is completed, the value of Y is different or 0. Therefore, when I can't understand who is before, when I don't know who, I will add a bracket to see the order. Let's make a parentheit practice: give statement c = a> b? A: b; plug. This statement has three operators: =,> ,?: How should I get parentheses?

First scheme: c = ((a> b)? A: b);

Second option: c = (a> (b? A: b));

Third solution: (c = a)> (b? A: b);

What should be the one? According to the high and low order of the operator priority,> The priority is higher than =, so it is impossible to enclose (c = a). And> The priority is higher than?: Operator. So it is impossible to enclose (b? A: b). Therefore, the first answer is correct.

Let's take another similar example:

INT i = 8, J = 4, K;

K = i

Suddenly, some people may have to calculate i and j. Here you may wish to take a look at it first. From the left to right, <级 高 = = =? 高??:::::::???????????????????????? :, So K = (i

So the priority of the operator must be careful, neither it is as difficult as it is, nor is it easy to imagine.

转载请注明原文地址:https://www.9cbs.com/read-131226.html

New Post(0)