Swift - Operator Arithmetic Operators


Swift supports the four standard arithmetic operators:

  • Addition +
  • Subtraction -
  • Multiplication *
  • Division /

Swift requires the operands in all arithmetic operations to be of the same type.

Consider the following statements:

var a = 9     //Int
var b = 4.1   //Double

By using type inference, a is Int and b is Double.

Because they are of different types, the following operations are not allowed:

print(a * b)  //error
print(a / b)  //error
print(a + b)  //error
print(a - b)  //error

You need to convert the variables to be of the same type before you can perform arithmetic operations on them.

Related Topics