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

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

Jhoan Arango
Jhoan Arango
14,575 Points

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