Operators are used in computer languages to perform various operations on variables. It could be an arithmetic operation (such as addition), a comparison operation to check whether a number is larger or smaller than the other, a logical operation to check whether a condition is true or false, or an arithmetic assignment such as increasing or decreasing a value. Let's now look at each type in detail.
Computers were initially made to perform numerical operations such as addition, subtraction, multiplication, and division. In Swift also, we have operators such as +
, -
, *
, /
, and %
.
Let's declare two variables, a
and b
, and initialize them with values 36
and 10
. If you want to make a single-line initialization in Swift, you can do it as shown in the following snippet of code:
var a = 36, b = 10
You can also use the semicolon to separate them, as shown here:
var c = 36; var d = 10
This is useful if you want to initialize variables of different types. Now let's use operators...