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 trialZach Perry
1,303 PointsThe concepts in Operators and Expressions, have me completely lost ...
I have watched this video a couple times, and have been trying to wrap my head around the concepts and code. The instructor runs through it all pretty fast. I was wondering if someone may possibly be able to help explain some of the things in this video such as int d= ++a; , with the incrementing A. REALLY LOST. Any help is greatly appreciated. Thank you for your time
3 Answers
Jeremy Hayden
1,740 PointsZach,
Lets break down that code: d = ++a
First, d =
This is how you assign any value to the variable named d. It could be d = 5, or d = 25. Whatever value you want it to be. Or you can do this, d = 5+6. d would then equal 11. So far easy stuff.
Next, the a. Lets go thru the same steps but with a.
Lets pretend a=5 We can say d = a, or d = 5 Or we can say d = a +1, or d = 6.
Last, the ++
Sometimes in your code you need a number to increment. That is increase by one. Think of a game app. You shoot a bad guy and your score goes up by one.
Could be written like this, score = score + 1
++ or -- is just a shortcut for us lazy coders it means plus 1 or minus 1.
So we can shorten score=score+1 to score++
You can put the ++ or -- on either side of your variable depending on when you want it incremented.
Lets put it all together.
d= ++a. Says first, take a and increment by 1, then thats what d equals. So if a = 5, then we would increment a to 6. Then make d = a or 6.
If we put the ++ on the otherside of the a like this d = a++, what happens?
d = a++ says first d = a or 5, THEN increment a after to 6. Leaving d with the old value of 5.
a++ says add 1 after checking value ++a says add 1 first then check value
Hope this helps
dsv97sdf789
2,166 PointsOMG. this lesson got me bummed.
dsv97sdf789
2,166 PointsAhh, so the lesson actually was missing that explaining part. Hmm..
Thanks Jeremy!
Curtis Curry
717 PointsYeah I began second guessing my decision to take up iOS development after watching this video. I felt like I was in a different country trying to understand a foreign language! lol
Bennett Sampson
Courses Plus Student 1,265 PointsThanks A Lot!!
Zach Perry
1,303 PointsZach Perry
1,303 PointsThis helped clear my confusion COMPLETELY. Thank you so very much for that very clear explanation!
Joseph Taralson
1,737 PointsJoseph Taralson
1,737 PointsThank you Jeremy I was in same boat as Zach and this helped a ton!
Balkaran Samra
1,341 PointsBalkaran Samra
1,341 PointsThanks you, I was confused by the video as well :)
Curtis Curry
717 PointsCurtis Curry
717 PointsThanks Jeremy, very solid explanation.
Ali Amirazizi
16,087 PointsAli Amirazizi
16,087 PointsThank you for this explanation. I've been trying to wrap my head around pre/post incrementing for an hour but I think I finally have it down.