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

John Moron
John Moron
1,658 Points

What is the value of totalScore? var totalScore = 10 - 20 ++totalScore

I don't understand this question.

3 Answers

Logan R
Logan R
22,989 Points
What is the value of totalScore? 
var totalScore = 10 - 20 
++totalScore

So what is happening is it is declaring a variable called totalScore. Total score is initialized with (10 - 20), so the initial value of totalScore is equal to -10.

With the ++ and -- operator, it is the same as saying:

totalScore = totalScore + 1

It is just a really shortened way of saying it.

The answer to the question is -9.

var totalScore = 10 - 20 // This equals -10
++totalScore             // This takes -10 and adds one to make -9

Hi John

If you put this into a playground file you will get the answer

var totalScore = 10 - 20  \\ - 10
++totalScore \\ -9
John Moron
John Moron
1,658 Points

ohh I got it now thanks.

Logan R
Logan R
22,989 Points

No problem! If you need any further clarification, just simply ask :)