Swift Arithmetic Operators

Swift Arithmetic Operators

Use +-*, and / to add, subtract, multiply and divide.

Integer division truncates toward zero.


Examples

These examples show addition, subtraction, multiplication and integer division.

Example

let a = 7, b = 3
print(a - b)
print(a * b)
print(a / b)        // integer division

This example demonstrates arithmetic with integers.



Remainder Operator

The remainder operator % gives the leftover after integer division.

Example

print(7 % 3)   // 1
print(8 % 2)   // 0
print(10 % 6)  // 4

Use % to check divisibility or cycle through a fixed range.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *