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

Andre Costa
Andre Costa
3,559 Points

So in this : 3,3%7 = ? Why 3,3 modulo 7 is equal to 3 ?

Probably this is more math than programming, but still, how does this work?

Many thanks

2 Answers

Thomas Nilsen
Thomas Nilsen
14,957 Points

The way modulo works is:

When you see an expression like 3,3 % 7, then ask yourself:

How many times can you fit 7 in 3,3? The answer is abviously 0 times. Leaving a remainder of 3,3.

Another example would be 10 % 3 = 1. How many times can you fint 3 within 10? 1 times is 3. 2 times is 6. 3 times is 9. Leaving a remainder of 1.

Andre Costa
Andre Costa
3,559 Points

Thanks man !

"How many times can you fit 7 in 3,3? The answer is obviously 0 times. Leaving a remainder of 3,3."

Now I get it! Many thanks!

SivaKumar Kataru
SivaKumar Kataru
2,386 Points

In Traditional Programming C , the modulo/remainder operator when applied to the Value % Divisor , the value should be greater than divisor . If the value is less than the divisor, then the result of the expression is either 0 or -1 (Depends on Compilers) . Example : 1 % 5 -> 0 // Compiler checks the VALUE vs DIVISOR first ,then starts evaluating the expression Example 2 : 25 % 5 -> 0

Modulo operator in Swift is Pretty powerful , this operator(%) is very useful to find whether the number is EVEN or ODD .

Example : 25 % 2 -> 1 (ODD NUMBER) Example 2 : 50 % 2 -> 0 (EVEN NUMBER)

This helps us to sort list of numbers in Even / ODD order .