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 trialJanson Roberts
5,071 PointsDifference 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
Treehouse Moderator 37,862 PointsThe ++
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
5,071 PointsIt'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
5,071 PointsThanks, 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. =)
Caleb Kleveter
Treehouse Moderator 37,862 PointsCaleb Kleveter
Treehouse Moderator 37,862 PointsSwift or Obj-C?