Swift Self-increment and self-increment operations

Source: Internet
Author: User

1 self-increment and self-increment operations2 3Like the C language, Swift also provides the convenience of adding 1 or minus 1 to the variable itself (+ +) and self-subtraction (--) operator. Its operands can be shaped and floating-point. 4var i =05++i//Now i = 16A value of ++i,i is added 1 per call. In fact, ++i is a shorthand for i = i + 1, and i = i = i-shorthand for 1. 7 8+ + and--both pre-and post-op. ++i,i++,--i and i--is a valid notation. 9 Ten It is important to note that these operators have a return value after I have modified I. If you only want to modify the value of I, then you can ignore the return value. But if you want to use the return value, you need to be aware that the return value of the pre-and post-operation is different.  OneWhen + +in front of the time, the first self-re-return.  AWhen + +Back to the time, the first to return to the self-increment.  -  - For example: thevar a =0 -Let B = ++a//A and B are now 1 . -Let C = a++//A is now 2, but C is the value of a self-increment before 1 -The above example, let B = + +a adds 1 to the first and returns the value of a. So both A and B are the new value 1.  +  -and let C = a++, the value of a is returned first, then a is added 1. So C gets the old value of a 1, and a plus 1 turns 2.  +  AUnless you need to use i++ 's features, it is recommended that you use ++i and-I, because it is more consistent with our logic to return this behavior before modifying it.

Swift Self-increment and self-increment operations

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.