Modulus - why this concept?

Ok. I am taking the 14 Day Beginner Challenge and on Lesson 6 I am getting lost. How does the following work?

// Modulus
var e = 20 % 2

which results in 0 ???

I googled it and got further confused that the % key in math is different from the % key in computer language and this key can mean different things depending on the computer language. Could someone explain the modulus concept and why it is important?

There is a good explanation here:

Thanks for the link. This concept is very difficult to wrap my head around and how this relates to Swift or Xcode. I will keep plugging away at this but Lesson 6 and 7 are over my head. I will watch both lessons again and see if there is anything I can understand that makes any sense. As I mentioned in my initial post, what has this got to do with understanding how to code? I have always had trouble wrapping my head around algebra.

I just tried to explain the modulo in my words, but as English is not my native language, I gave up after 10 minutes :cold_face:

But I found another good explanation:
https://www.hackingwithswift.com/example-code/language/how-to-calculate-division-remainder-using-modulo

And here are real world examples when to use the modulo:

A very common task is the question, how many hours, minutes, and seconds are 582 seconds?
With the modulo, you can easily determine the answer (see link above).

Do you need to understand modulo to understand SwiftUI? Probably not. It’s just an example which is a little bit more advanced than 5 + 2 or 10 / 5.

In the end, a lot of coding will include math.

1 Like

There’s no need to fully understand modulo to know how to program. (Honestly go ahead and skip it if it doesn’t make any sense)

A lot of times it’s nice for when you want to do something every other time, and you use modulus to know whether a number is even or odd, to figure out the “other time”

It’s not directly related, it’s a programming concept that can be used in any programming language, whether or not you need it for a program you make just depends

1 Like