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 2.0 Basics Swift Operators Working With Operators: Part 2

What is the answer?

?

operators2.swift
// Enter your code below

var initialScore = 8
initialScore = initialScore + 1

var totalScore = initialScore
totalScore = ++initialScore
Mazen Halawi
Mazen Halawi
7,806 Points

Both variables values will be equal to 10.

keep in mind there is a big difference between ++variable and variable++ ++variable will force the incrementation of the variable before you start using it in the line, whereas variable++ will cause the increment to be after you are done with the calculation on that line (more like deferred addition) example:

var sum = 20 var total = sum++ (will result in total to be 20 and sum will become 21 on the next line because the ++ will only fire after all calculations are done on this line)

however:

var total = ++sum (will result in total and sum to be 21 on this line and afterwards because the ++ before the variable will trigger the sum to increment first and then be assigned to total)

hope its clear! cheers.

Yes, thanks!