[Note] C # BASICS (13th) -- operator priority of C,
If multiple operators are used in programming, which one will calculate first? This is the priority issue.
For the priority of the C # Operator, see the following sequence:
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 called binary operators. Only one operand is called The unary operator, and their priority is higher than the binary operator. Unary operators include: ++ (auto-increment), -- (auto-Subtract), and ),! (Not logically ).
③ * (Multiplication),/(Division), and % (remainder ).
④ + (Plus),-(minus ).
⑤> (Greater than), <(less than), >=( greater than or equal to), and <= (less than or equal ).
⑥ = (Equal ),! = (Not equal ).
7 & (logical and ).
Logical | (logical or ).
Assign value operator. Including: =, + =,-=, * =,/=, and % =.
In addition, you must note that 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;
Of course, the final result is false.
The above is taken from MOOC course C # getting started with development