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
ub46473788
8,782 PointsMethod of incrementing "counter" var -- counter += 1; vs. counter ++; - Why?
In the "What are Loops?" video, Dave increments the 'counter' variable on line 6 with "counter += 1;" I'm wondering if there's a reason to prefer this method to the slightly more economical "counter ++;"? Can anyone shed some light on this?
1 Answer
Ferdinand Pretorius
18,705 PointsThere is no difference whatsoever on a technical side. It is likely that Dave either comes from a background where the ++ operand is not supported ( python for example ), or he is keeping things simple for those who might come from a different background.
Either way, I'd say just stick to which ever you prefer.
Hope this helps!
ub46473788
8,782 Pointsub46473788
8,782 PointsExcellent. Thanks very much, Ferdinand.
Ferdinand Pretorius
18,705 PointsFerdinand Pretorius
18,705 PointsA friend just mentioned to me that he prefers += because if you want to increment with more than 1 it's easier to make the adjustments.
Too be honest though, it's not too often you have to increment by more than 1 at a time, and I feel the adjustment need is so small that I personally will stick to ++.
You're welcome.