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 Operator Precedence

Laz Alberto
Laz Alberto
284 Points

Operators not working?

whenever I type an operator with more than one equation (1+1 works fine, for example, but not 100 + 100 - 5 * 2 / 3 % 7) I get no result... what is happening!

Aaron Batchelder
Aaron Batchelder
3,240 Points

Hey Laz. When you start writing a bunch of numbers like that with random operators you run into the order of operations. PEMDAS - Parentheses, Exponents, Multiplication, Division, Addition, Subtraction. It's good to figure out how exactly you want the equation to execute before writing it. Keep in mind you can use parens to override the order of operations because P - Parentheses is executed first.

Laz Alberto
Laz Alberto
284 Points

Thanks Aaron, that's good to know. The problem actually resolved itself though. I closed xcode for a few hours and when I reopened, the equation worked as it was! Thanks a lot for responding, though!

1 Answer

Dave Berning
Dave Berning
17,365 Points

Try using parentheses in your statement

var x = 1 + ((2 * 3) % 4)) // random numbers just for the example

if x < 5 {
     // then do something
} else {
    // then do something else
}
Laz Alberto
Laz Alberto
284 Points

Thanks Dave, that's good to know. The problem actually resolved itself though. I closed xcode for a few hours and when I reopened, the equation worked as it was! Thanks a lot for responding, though!