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 Swift Basics (retired) Operators Unary Operators

why does var levelScore = 0 and then performing levelScore++ net you a return of 0 instead of a value of 1?

I understand that the ++ -- are incremental. However if the levelScore is set to 0 and in the following line there is a statement, levelScore++ instead of the answer being 1 it is Zero not 1. Why is this? I don't feel like instructor answered this question. Maybe he did in a round a bout way but I'm missing something. Thank you to anyone who can give a clear answer to this.

3 Answers

Using increment and decrement operators before or after the variable will result in different rules applying. From Apple's documentation:

If the operator is written before the variable, it increments the variable before returning its value.
If the operator is written after the variable, it increments the variable after returning its value.

For example

var a = 0
let b = ++a
// a and b are now both equal to 1
let c = a++
// a is now equal to 2, but c has been set to the pre-increment value of 1

Or with your example

var levelScore = 0
let score = levelScore++
//score is equal to 0
//levelScore has been set to 1, since the operator is after (or postfix)

Source

Thank you Sean I now better understand how the increments work. But this opened a new question. Why would you even code this line

let score = levelScore++ Yes it increments levelScore but the Score variable never changes from zero. You would never use Score for anything in particular if it's score is always zero. Seems kind of useless to me to do all that work to increment levelScore through the use of another variable. Why would you ever need to increment a one variable through the use of yet another variable that never actually changes. Seems like a waste of code. Maybe everything here would make sense in the actual creation of a game or program. But as it stands with no context, it seems to fall flat as a perfect example for teaching someone about Unary Operators. I understand better about incrementation now but not for the actual real world use of some of this logic.

If you need to store a variable of a value, before incrementing, you would use

var a = 1
var b = a++
// a is now 2, but b is equal to 1
b = a++
//a is now 3, and b is equal to 2
// this is because the value is set to before the increment, as denoted by the postfix operator.

Say you have a game where your score reverts to the previous value when the player is hit. You can temporarily store a variable and increase the score, like in the example, then have the score equal to that declared variable.

Ah most Excellent Example Sean. Thank you so much for clearing that last part up. I can think of these value increments as either temporary or non temporary uses. The original variable, the one being incremented, is always incremented, prefix use is so that it affects the second variable as well.

Thank you!