Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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. =)