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

Unary code result

if "levelscore = levelscore + 1" returns a result of 1, why does levelscore++ (which as explained in the video is technically the same as "levelscore = levelscore + 1") return a result of 0? If it's the same shouldn't the result be 1?

5 Answers

The value declared is 0. var levelScore = 0

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

When you use a unary operator you call it, then add 1. So just type levelScore under levelScore++ and it will show the value 1.

I don't understand why I would need to type levelScore again below?

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Because it calls the variable, then adds 1 to the number.