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

I typed out the equation into Xcode 2 ways and the answer was 197 both times. Does Swift 3 evaluate differently than 2?

I'm wondering if Swift 3 evaluates Operator Precedence differently than Swift 2. In the exercise were were asked to type out two equations, one with parentheses so that Xcode can evaluate different numbers in and order other than left to right. Yet when I typed the problem var x = 100 + 100 - 5 * 2 / 3 % 7 and var x = 100 + 100 - ((5 * 2) / 3) % 7 the answer came out to be 197 on both equations. Is Swift 3 evaluating things differently than Swift 2?

1 Answer

Unfortunately I don't have Swift 2 on my devices anymore, but I really don't think precedence has changed. Actually, both equations are exactly the same and should return 197. You can omit the parenthesis and nothing would be different, they just emphasize what precedence looks like.

var x = 100 + 100 - 5 * 2 / 3 % 7
var y = 100 + 100 - ((5 * 2) / 3) % 7

Ah ok. I guess I was thinking that the video was inferring that the equation would be different based on the parentheses emphasizing a different precedence for each equation. Thanks Martin!