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 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 :)