Precautions for auto-increment and auto-subtraction in Arithmetic Operators

Source: Internet
Author: User

++ And -- both operators are single-object operators, which have the right combination (that is, the combination of priority and the variable on the right of the operator), and their priority is higher than other arithmetic operators. When the ++ or -- operator is placed on the left of the variable, it is called a prefix operation, indicating that the value of the variable is used after the auto-increment or auto-subtraction operation, when the ++ or -- operator is placed on the right of a variable, it is called a post-operation, indicating that the value of the variable is referenced before auto-increment or auto-subtraction.

For example, if I = 1, j = 2, The ++ I + j -- operation is performed first, And I = 2 is obtained, then perform the j -- operation. According to the post-operation rules, the system adds the original value 2 of J and the new value 2 of I, and then performs the j --, and obtains j = 1. therefore, the expression value is 4.

Private void hellofrm_load (Object sender, eventargs E)
{

Int I = 1, j = 1, p, q;
P = (I ++) + (I ++ );
Q = (++ J) + (++ J );
Label1.text = "I value:" + I + ",";
Label1.text + = "J:" + J + ",";
Label1.text + = "P value:" + P + ",";
Label1.text + = "Q:" + q;
}

Result After running: the value of I is 4, the value of J is 4, the value of P is 6, and the value of Q is 9.

 

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.