Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Janson Roberts
Janson Roberts
5,071 Points

Difference in using certain operators (++,--,+=)

If I were to take a for loop and start with a value of (int i = 5; i <= 25; i++){ mathTotal += i; }

Could someone explain what the operand is (+=) and how it differs from ++ or x = x + y?

3 Answers

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

The ++ and -- operators only increment/decrement by 1. += is short-hand for x = x + y. It would be used if you want to increment by 2, for example.

Janson Roberts
Janson Roberts
5,071 Points

It's for Obj-C. That's nice to know for Swift. My thinking is that it's just a simplified way to write x = x + y...is that correct?

Janson Roberts
Janson Roberts
5,071 Points

Thanks, Caleb.

So, if I wanted to increment or decrement by 2 or more, then I'd have to write (x += 2)? Just wanting to get a better understanding of the operators used to increment/decrement. The ++ and -- TOTALLY make perfect sense. I'm just trying to wrap my head around the += or -= operators. =)