Swift Subtraction Operator
To perform subtraction of two numbers in Swift, we can use Swift Subtraction Arithmetic operator.
- takes two numbers as operands and returns the difference of second operand from the first operand.
The syntax of Subtraction Operator is
a - b
b are left and right operands respectively.
These operands should be of same type.
bcan be Int.
bcan be Float.
bcan be Double.
1. Subtraction of integer values
In the following program, we will perform subtraction of two integers.
var a = 10 var b = 4 var result = a - b print("The result is \(result)")
2. Subtraction of Double values
Now, let us find the subtraction of two Double values.
var a = 10.2 var b = 4.1 var result = a - b print("The result is \(result)")
2. Subtraction with values of different datatypes
If we try to subtract numbers of different types, we would get Swift Compiler Error as shown below.
a is Double and
b is Int, Subtraction
- operator raises Compiler Error.
In this Swift Tutorial, we learned how to perform subtraction in Swift programming using Subtraction operator. We have seen the syntax and usage of Subtraction operator, with examples.