SwiftAppleSnippetProgramming
Last updated at 2023-07-10

Understanding Swift Modulus Operator

ClickUp
Note
AI Status
Last Edit By
Last edited time
Jul 10, 2023 04:01 PM
Metatag
Slug
understanding-swift-modulus-operator
Writer
Published
Published
Date
Jul 5, 2023
Category
Swift
Apple
Snippet
Programming
In programming languages, you use the modulus operator to get the remainder value after division. If you divide 10 by 3, then the remaining number is 1. You get 1 using the modulus operator.
The modulus operator is represented by % in Swift. But be wary; it is more of a remainder operator than a modulus operator. But because it is intended as a modulus operation, we will use it interchangeably.
In Swift, there is a difference when you use the remainder operator with a negative number. The remaining value after the operator is applied will be different from what you expect.
In this tutorial, you will learn how to use the modulus operator, and in the end, you will understand when and how to use it correctly.

What is the Swift Modulus/Remainder Operator?

You use the remainder operator using % in Swift. This operator is also called the remainder operator. When we use this operator for two numbers or operands, it will result in a reminder of the division between those numbers. The formula of this remainder operator is like this:
Dividend = Divisor x Quotient + Remainder
The term dividend refers to the value that is going to be divided by another value that is called a divisor. The quotient is the result of the division, while the remainder is the value that remains after the division.
You can follow this code to illustrate how the modulus operator works. We will calculate 9 mod 4 as follows:
let quotient = 9 / 4 let remainder = 9 % 4
In this case, the quotient is 2, and the remainder is 1. Therefore, when 9 is divided by 4, the remainder is 1.

The Difference Between Remainder and Modulo

Using the modulus operator for a negative number is quite different. You can expect the sign of the remainder value to be positive when there is a positive dividend and a positive divisor. But it works differently if you have a negative dividend.
A negative or positive dividend will determine the result of the remainder operation. A positive dividend will result in a positive remainder, and a negative dividend will result in a negative remainder. The sign of the right operand is ignored in the remainder operator.
The above behavior can be seen in this example:
9 % 4 // Result: 1 9 % -4 // Result: 1 -9 % 4 // Result: -1 -9 % -4 // Result: -1
Both operands are positive in the first operation, so the remainder is 1. In the second example, because the left operand is positive, the remainder is still 1, even though the right operand has a negative sign. A negative remainder results when the left operand is negative, as in examples 3 and 4.

Using the Swift Modulo Operator

The Swift modulo operator can be useful in various scenarios. Here are a few common use cases:

1. Checking for Divisibility

Checking that a number is divisible by another number can be done using the modulus operator. If a number can be divided by another number, the remaining value must be 0.
This example illustrates that:
let number = 15 if number % 5 == 0 { print("The number is divisible by 5") } else { print("The number is not divisible by 5") }
In this case, since 15 is divisible by 5, the output will be "The number is divisible by 5". If the number were not divisible by 5, the else block would execute.

2. Cycling Through an Array

Cycling through an array is another example of this modulus operator. You can loop and array infinitely only by using the remainder value of your current loop index with Array.count. That way, you will never get an array index out of bounds.
You can follow this code for the real code:
let colors = ["red", "green", "blue"] for i in 0..<10 { let color = colors[i % colors.count] print(color) }
In this case, the modulo operator ensures that the index wraps around when it exceeds the number of elements in the array. As a result, the colors will be printed in a circular manner.

Conclusion

The modulo operator in Swift is a tool that you may encounter on a day-to-day basis. It is useful to calculate the remainder of the division operator. One thing that you should note is that it behaves differently when dealing with negative numbers. Understanding how this operator works can help you perform various tasks. Checking divisibility and cycling array is only a little example, you can learn a lot more later.
Now that you already know how to use this operator, go ahead and do some modulus operations. Learn and test your understanding; you may draw some more perspective from what you see. If you would like, share with us!
Additional Information: The Swift modulo operator is a powerful tool for remainder-based calculations. This operator can be used in many applications, including mathematical operations, data manipulation, and algorithmic problem-solving. By mastering the modulo operator, you can boost the efficacy and precision of your Swift programs.
Read More

Discussion (0)

Related Posts