Detailed explanation of operator priority of C,
Operator priority of C #
①Brackets. When learning mathematics, we know that we need to calculate the content in the brackets first. The same is true for C #. If multiple parentheses exist, they must be calculated from the inside out. Brackets have the highest priority.
②Unary operator. Some operators have two operands, such as 2 + 3 and 6% 5, which are calledBinary Operators. Only one operand is calledUnary operatorThey have a higher priority than binary operators. Unary operators include:++ (Auto-increment), -- (auto-increment), and ),! (Non-logical).
③* (Multiplication),/(Division), % (remainder).
④+ (Plus),-(minus).
⑤> (Greater than), <(less than), >=( greater than or equal to), <= (less than or equal).
⑥= (Equal ),! = (Not equal).
7.& (Logical and).
Bytes| (Logical or).
BytesValue assignment operator. Including: =, + =,-=, * =,/=, and % =.
Note the following:Operators with the same priority are calculated from left to right (the assignment operator is opposite).
See the following code:
bool b = 20 - (15 - 8) * 2 > 10 && (2 % 2 * 2 + 2) > 2;Console.WriteLine(b);
Analysis: first, calculate the brackets with the highest priority. (15-8) get 7. (2% 2*2 + 2) Calculate % and * first, and then calculate +, if the result is 2, the expression is changed:
bool b=20-7*2>10&&2>2;
Next, the highest priority is 7*2, followed by subtraction, and changed:
bool b=6>10&&2>2;
Continue to calculate the two greater than signs, and get:
bool b=false&&false;
The final result is false: