[Note] C # BASICS (13th) -- operator priority of C,

Source: Internet
Author: User

[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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.