C + + self-increment and decrement operators (--and + +)

Source: Internet
Author: User

In C and C + +, the self-increment (+ +) and decrement (-) operators are often used in expressions to increase the value of a variable by 1 or minus 1, for example:
++i (before using I, the value of I is added 1, if the original value of I is 3, after the execution of J=++i, the value of J is 4)
-I (Before using I, the value of I is reduced by 1, if the original value of I is 3, then after the execution of J=--i, the value of J is 2)
i++ (after using I, the value of I is added 1, if I is the original value of 3, then after the execution of j=i++, the value of J is 3, then I becomes 4)
i--(after using I, the value of I is reduced by 1, if the original value of I is 3, then after executing j=i--, the value of J is 3, then I becomes 2)
++i is the value of I after the i=i+1 is executed, and i++ is the value of I before the i=i+1 is executed.

Correct use of + + and--can make the program concise? clear, efficient. Please note:

    1. The increment operator (+ +) and the decrement operator (--) can be used only for variables, not constants or expressions.
    2. + + and--the combination of the direction is "from right to left."
    3. The self-increment operator (+ +) and the decrement operator (--) are very flexible to use, but in many cases there may be ambiguities that produce "unexpected" side effects.
    4. The increment (minus) operator is often seen in C + + programs and is often used in loop statements to automatically add 1 to the loop variable. Also used for pointer variables to point the pointer to the next address.

C + + self-increment and decrement operators (--and + +)

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.