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

I do not get the postfix ++ unary operator

Why does this give me 0 in the result pane, but when I type it again, it gives me 1. Take a look!

var scoreExample = 0 scoreExample++ scoreExample //when I type it again, it is 1... why?

2 Answers

Egor :

The operator ++ increments values one by one. Here is an example

var someScore = 0  // someScore is now 0

// if I want to increment “someScore” by ONE, I can do it like this

someScore++ // this will equal now to 1

// if I do it again 

someScore++ // this will equal 2 now.

// Say that I now want to bring the “someScore” back to 0

someScore = 0 // this will RESET the someScore back to 0

The ++ operator increments a value by one, the -- operator decrements a value by one.

Hope that clears it