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 Swift Operators Working With Operators

What is the remainder operand? That was covered yet.

That was covered yet.

2 Answers

andren
andren
28,558 Points

The remainder operator, also commonly known as the modulo operator in other languages is: %.

And it is used in the same way you would other operators like +,-, etc.

So "5 % 2" for example would compute the remainder of dividing 5 by 2.

Thank you. Why does 3 % 7 result in 3? Per the code challenge.

andren
andren
28,558 Points

When doing integer division (ignoring decimals) 3 / 7 would be 0. the difference (remainder) between 0 and 3 is 3. So 3 is the remainder of that operation.

This essentially means that any modulo operation where the first number is lower than the second will result in a remainder equal to the first number.

  • 8 % 10 = 8
  • 97 % 352 = 97
  • 100 % 412 = 100

etc.

Got it...makes sense. Thank you.